Page 1 of 1

Inverse sorting a list

Posted: Sat Jan 11, 2014 9:37 am
by Mag
Hi all,

I'm wonder if there is a easy way to sort a list of return-delimited items in reverse order.

e.g.
New York
Paris
Lisbon
San Diego
Rome

Sould become:
Rome
San Diego
Lisbon
Paris
New York

:roll:

Re: Inverse sorting a list

Posted: Sat Jan 11, 2014 10:28 am
by Simon
Hi Mag,
Isn't this just sort container descending?

Simon

Re: Inverse sorting a list

Posted: Sat Jan 11, 2014 12:48 pm
by bn
Hi Mag,

if you want to inverse a list you have to have some way to index the lines of the list. There are numerous ways to do this. Some are faster and some are slower.
It depends on the size of your list. If you have a list with say up to 50 lines speed is not really important. If you want to reverse large lists it makes a big difference.

Slow is accessing a field by its line number repeatedly.

Fast is creating a array on the fly and use the index of the array to make the inverted list.

I attach a stack which shows 3 ways to do what you want, all take the data out of the field and do the inversion on a variable (faster).

Have a look at the scripts and pick any method that suits you or make a fourth, fifth etc one.

Kind regards
Bernd

Edit: replaced stack with corrected version. The code of button "invertList sort function" has been corrected. Sort had to be numeric to work with more than 9 lines. Thanks goes to Hermann [-HH] to catch this.

Re: Inverse sorting a list

Posted: Sat Jan 11, 2014 3:08 pm
by Mag
Hi Simon, sorry I forget to say that the list is not alphabetically sorted.

Hi Bernd , Thank you so much, very interesting stack (and extremely useful comments)! I will study the three different ways!

PS
I'm sorting a list of paths of files of photos to display in a mobile album, so I want to display them in "standard" order on iOS and inverse order in Android.

Re: Inverse sorting a list

Posted: Sat Jan 11, 2014 7:50 pm
by bn
Hi Mag,

Hermann [-HH] made me aware of a mistake I made in the code of the third button. The sort has to be numeric. I omitted numeric from sort. That means it will only work for less than 10 lines correctly.

I replaced the stack above with a corrected version.

Thanks goes to Hermann

Kind regards
Bernd

Re: Inverse sorting a list

Posted: Sun Jan 12, 2014 1:08 am
by Mag
Thank you Bernd!