122
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 :)


UserImage.jpg
Ellakkiya

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


2019-10-04 15-01-22
Dhiraj Manwani

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.

2022-10-17 06-51-09
Shweta Gedam

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


2019-10-04 15-01-22
Dhiraj Manwani

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.



2022-10-17 06-51-09
Shweta Gedam

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

2019-10-04 15-01-22
Dhiraj Manwani

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

2022-10-17 06-51-09
Shweta Gedam

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

2019-10-04 15-01-22
Dhiraj Manwani
2022-10-17 06-51-09
Shweta Gedam

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 :)

2019-10-04 15-01-22
Dhiraj Manwani

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?

2022-10-17 06-51-09
Shweta Gedam

Hi Dhiraj,

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

Hope it helps, Thanks and Happy Coding! :)

ListSortReverse.oml
2018-10-29 08-31-03
João Marques
 
MVP

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

2023-10-16 05-50-48
Shingo Lam

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

2026-04-01 11-40-30
Mahesh Manchala

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

2021-07-07 13-36-32
Hans Dollen

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  


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.