Page 1 of 1

sortKey

Posted: Thu Sep 27, 2012 10:13 am
by MMoeller
Hi fellow LiveCoders,

i have got a question regarding the sort function in LiveCode 5.5.1 running on MacOS 10.8.2.I need to generate a random order of lines in a variable. I usually use the following code:
(A)

Code: Select all

  sort lines of cSet1 by random(1000000) 


Now i wonder what happens exactly when I run this line. Is is so that the output of the random(1000000) function is a number between 1 and 1000000. So, the code might for example look like this:

(B)

Code: Select all

  sort lines of cSet1 by 594834 
? Or is that my first misunderstanding? Running the code (B) in the message box does return "true" but does not change the order of the items in the variable, but code (A) does...What does the sortKey element of the sort function mean? I am a bit puzzled. The dictionary was no help either...

Hope you can help me :-)

Cheers,

Malte

Re: sortKey

Posted: Thu Sep 27, 2012 12:14 pm
by Mark
Guten Tag Malte,

You're mistaken, but that's understandable. When you call the sort function, you call an internal repeat loop, which actually works like this:

Code: Select all

repeat for each line myLine in cSet1
  put random(1000000) into x
  put myLine & cr after line x of myNewList
end repeat
delete last char of myNewList
put myNewList into cSet1
put empty into myNewList
The real routine is slightly more complicated, because the order of items that are assigned equal x-values is preserved.

Kind regards,

Mark