sort an array with alphabet

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kimberlyn
Posts: 18
Joined: Mon Jul 01, 2013 1:40 pm

sort an array with alphabet

Post by kimberlyn » Tue Jul 02, 2013 10:57 pm

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 !

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

Re: sort an array with alphabet

Post by Simon » Wed Jul 03, 2013 1:51 am

Take a look at this:
http://forums.runrev.com/viewtopic.php?f=7&t=15568

Looks like it might not be possible?

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: sort an array with alphabet

Post by dunbarx » Wed Jul 03, 2013 3:57 am

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

kimberlyn
Posts: 18
Joined: Mon Jul 01, 2013 1:40 pm

Re: sort an array with alphabet

Post by kimberlyn » Wed Jul 03, 2013 9:10 am

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

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: sort an array with alphabet

Post by shaosean » Wed Jul 03, 2013 11:35 am

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

Post Reply