sortKey

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
MMoeller
Posts: 13
Joined: Tue Apr 19, 2011 12:30 pm

sortKey

Post by MMoeller » Thu Sep 27, 2012 10:13 am

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

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

Re: sortKey

Post by Mark » Thu Sep 27, 2012 12:14 pm

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
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

Post Reply