# Searching backwards through an array?

How can I search backwards through an array? Or are there other ways I can solve this problem?

I have a text array, stored in a field, that is more or less like this:

``````Satina OG
Yukon Gem OG
Red Maria OG
Desiree OG
Magic Molly
Chieftain
Carola
``````

Without altering the array or using variables, I want to display this array in a Text Display SuperObject so that it looks like this:

``````ORGANIC

Satina OG
Yukon Gem OG
Red Maria OG
Desiree OG

CONVENTIONAL

Magic Molly
Chieftain
Carola
``````

I can achieve this with the following formula in my SuperObject:

`"ORGANIC"+ ¶ + ¶+ arrayrange(MyArray,1,4,¶)+ ¶+ ¶+"CONVENTIONAL" + ¶+ ¶+ arrayrange(MyArray,5,30,¶)`

However, the length of the array varies from record to record, so the `firstitem` and `lastitem` parameters in `arrayrange()` can’t be constants. I thought to use `arraysearch()` to look for “* OG”, but what I really need to do is find the last occurrence of OG, or rather, to search from the end of the array. So if there were a function called “`arraysearchbackwards()`”, my formula would look something like this:

`“ORGANIC”+ ¶ + ¶

• arrayrange(MyArray,1,arraysearchbackwards(MyArray,"* OG",1,¶),¶)
• ¶+“CONVENTIONAL” + ¶+ ¶
• arrayrange(MyArray,arraysearchbackwards(MyArray,"* OG",1,¶)+1,10,¶)`

Since this function only exists in my imagination, can anyone offer an alternative way to achieve this?

An equivalent formula to your arraysearchbackwards( function would be

``````arraysize(MyArray,¶) - arraysearch(arrayreverse(MyArray,¶),"* OG",1,¶) + 1
``````

You could use that formula to define a custom function if you wanted to.

My solution would take a different approach and use the `arrayfilter(` function instead. This formula will first consolidate all elements that end with “OG” and then all those that do not. It would not matter how many of each there are or even if they are interspersed within each other.

``````"ORGANIC"+ ¶ + ¶+ arraystrip(arrayfilter(thelist,¶,{?(import() endswith "OG",
import(),"")}),¶)+ ¶+ ¶+"CONVENTIONAL" + ¶+ ¶+ arraystrip(arrayfilter(thelist,¶
,{?(import() endswith "OG","",import())}),¶)``````

Excellent! Somehow I was thinking I wouldn’t be able to use arrayreverse in a SuperObject, but this works perfectly.

@gary Thank you for another approach!