Can you sort an array

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
el_stupido
Posts: 24
Joined: Tue Jun 23, 2009 9:38 am

Can you sort an array

Post by el_stupido » Tue Nov 03, 2009 8:14 am

Can you use the sort command on an array?

or is the a separate sort command for arrays?

or do I have to pass the array to a variable then sort it???????????

Just seeing if I can cut corners...


:twisted:

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Tue Nov 03, 2009 10:35 am

Dear el_stupido,

You can't sort an array, because it has no order. You can put the keys of an array into variable and sort them:

Code: Select all

put the keys of myArray into myVar
sort lines of myVar
repeat for each line myLine in myVar
  put myVar[myLine] into myFoo
  -- do something with myFoo
end repeat
If this isn't what you want, then you will have to combine the array and deal with it as an ordinary variable.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

el_stupido
Posts: 24
Joined: Tue Jun 23, 2009 9:38 am

Post by el_stupido » Tue Nov 03, 2009 2:59 pm

I meant the values stored in the array say for instance 'sort the values of this array in ascending order.

I'm guessing no command for this though. Looks like I have to code the sort the long way :(

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Post by BvG » Wed Nov 04, 2009 2:09 am

you can do it using split and combine (make sure to use delimiters that fit your data):

Code: Select all

combine theArray by return and comma
sort theArray by item 2 of each
--do stuff for which you need it sorted
split theArray by return and comma
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

skindoc4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 77
Joined: Mon Jul 07, 2008 1:22 am

Re: Can you sort an array

Post by skindoc4 » Sun Nov 29, 2009 10:12 am

I also discovered this property, rather lack of a property, the hard way when my database fields failed to line up the way I expected them to. I sort the keys as a reflex now.

Alex

Post Reply