25
Views
15
Comments
For each loop in descending order.
Question

Hi All, 


I want to process a list in for each loop in reverse order i.e. descending order of the list,

Last index to be processed first.

Any idea how can i achieve this?


Thanks in advance :)


Hi Dhiraj Manwani,

 If you are using aggregate to iterate, then you need to sort your id according to your requirement

Instead of aggregate if you are using variable of list datatype then you need to use list sort action


Rank: #1158

Thanks for reply;

i do not have id for sort by, i a getting the response from API, and i do not have any sorting parameter, i just need to reverse the list in the order it is received.

Rank: #290

Hi Dhiraj,

You need to use List sort and sort it in descending order by Id of list and then apply For each loop on that list.

Make sure to make IsAscending to false


Rank: #1158

Thanks shweta,


i do not have id for sort by, i am getting the response from API, and i do not have any sorting parameter, i just need to reverse the list in the order it is received.



Rank: #290

Okay, so are you getting any unique incrementing index to sort  the list in descending?

Rank: #1158

No i am not getting any such index in the response, so hat i can sort.

I want to sort the list itself on the descending order, irrespective of any parameter in the list.

Hope i am bit clear now.


for refrence

Rank: #290

Hi Dhiraj,

To solve your sort problem, you can create one attribute of type integer in your local list (using structure). and append the incremented values starting from 1 in that attribute.

At the last use ListSort action and sort it by the attribute newly created, then apply loop over to it.

Hope you are bit clear. Thanks

Rank: #1158
Rank: #290

Inside preparation, assign your original list to the local variable list as follows:

And then, inside your screen action, use List Sort and sort the id of that list in descending order.

Hope this helps! Thanks :)

Rank: #1158

Hi Shweta,


I tried the same assigning it to a local variable and then sort,

But still i am not getting any param, on which i can sort.


Also, i am trying to sort the list i am getting from an api response, it has a specific structure, but no param as ID.


Any other idea?

Rank: #290

Hi Dhiraj,

Here i have attached a sample oml for your scenario.  Please refer it.

Hope it helps, Thanks and Happy Coding! :)

ListSortReverse.oml

Rank: #68

Hi Dirhaj,


You can do a for each on your list and insert element by element in a new list, but always in the first position, in this way, the first you insert will be pushed to the end of the list and the last will be inserted in the beginning. For that you would be using the ListInsert action from System:



So step by step, you would have a list local variable (in my example eSpaceList) of the same type of the list you want to order (in my example query GetEspaces).

Then you do a for each on the output of your web service (in my case a query) and insert in the local variable list that element in position 0. Here's how it looks like:



In the end of the for each, your local variable (in my example eSpaceList) will be a copy of the other but with the order reversed.


Hope it helps.


Cheers,
João

Rank: #493

Yeah, that's it. Just foreach from top, but insert everything on top at first position

Rank: #259

Hi,

you can refer the below documentation also:

https://success.outsystems.com/Documentation/11/Developing_an_Application/Use_Data/Query_Data/Sort_Results_in_an_Aggregate

Rank: #361

If you can't order the list in the database and won't have extra overhead of sorting the list in memory than you can use another alternative by looping through a list like 

https://www.outsystems.com/forums/discussion/53044/how-to-create-a-loop-but-not-for-each-loop/ 

But, of course, you have to start with counter = [list.length-1] 

if counter >= 0 than you can run your logic on the list[counter] element and decrease the counter with 1. 

regards Hans