Page 1 of 1
sort an array with alphabet
Posted: Tue Jul 02, 2013 10:57 pm
by kimberlyn
Hi everyone !
Like mention in the title, I have a little question I couldn't answer by myself.
Is there a simple way to sort an array containing words with the alphabet ?
I've tried to make some research but I couldn't find something.
If someone could explain me it will be great
Bye !
Re: sort an array with alphabet
Posted: Wed Jul 03, 2013 1:51 am
by Simon
Take a look at this:
http://forums.runrev.com/viewtopic.php?f=7&t=15568
Looks like it might not be possible?
Simon
Re: sort an array with alphabet
Posted: Wed Jul 03, 2013 3:57 am
by dunbarx
Hi.
The keys of an array in LiveCode are associated with their elements, but the order is not preserved. in other words, if you create an array from data which is sorted a certain way, you may no longer find it sorted once you examine the array, or if you then extract the data from that array. All the associations will be intact, just not the order. When you deconstruct the array using the "combine" command to change the array variable into an ordinary variable, then you can sort in any way you wish.
In the end, the usefulness of sorting generally is only pertinent once the data is exported or displayed somewhere, either in a variable or in a container like a field. Sort when you need to at those times.
Craig Newman
Re: sort an array with alphabet
Posted: Wed Jul 03, 2013 9:10 am
by kimberlyn
Hi,
I'm going to look deeper into that question.
Thanks for your help. At these moments I'm realizing that even LiveCode isn't that perfect :p
Re: sort an array with alphabet
Posted: Wed Jul 03, 2013 11:35 am
by shaosean
This will take an array, sort the keys and return the values in a LF-delimited variable.. Obviously if the values are not single lines, you will want to change the delimiter used in the return data.. Also, I just typed this in off the top of my head, so hopefully it should work the way I was thinking it would
Code: Select all
function getArrayDataSortedByKey(pArray)
local tKeys
put the keys of pArray into tKeys
sort lines of tKeys ascending numeric
local tReturn
repeat for each line tKey in tKeys
put pArray[tKey] & LF after tReturn
end repeat
delete char -1 of tReturn
return tReturn
end getArrayDataSortedByKey