Inverse sorting a list

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Inverse sorting a list

Post by Mag » Sat Jan 11, 2014 9:37 am

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:

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Inverse sorting a list

Post by Simon » Sat Jan 11, 2014 10:28 am

Hi Mag,
Isn't this just sort container descending?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Inverse sorting a list

Post by bn » Sat Jan 11, 2014 12:48 pm

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.
Attachments
inverseList.livecode.zip
(1.57 KiB) Downloaded 175 times
Last edited by bn on Sat Jan 11, 2014 7:48 pm, edited 1 time in total.

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Inverse sorting a list

Post by Mag » Sat Jan 11, 2014 3:08 pm

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.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Inverse sorting a list

Post by bn » Sat Jan 11, 2014 7:50 pm

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

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Inverse sorting a list

Post by Mag » Sun Jan 12, 2014 1:08 am

Thank you Bernd!

Post Reply