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
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.
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
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.
Okay, so are you getting any unique incrementing index to sort the list in descending?
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
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
Thanks Shweta,
I got you :)
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 :)
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?
Here i have attached a sample oml for your scenario. Please refer it.
Hope it helps, Thanks and Happy Coding! :)
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
Yeah, that's it. Just foreach from top, but insert everything on top at first position
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
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