[TIP] Sorting by ValueList and synchronized sorting

J. Landman Gay jacque at hyperactivesw.com
Tue Jun 17 17:36:08 CDT 2008

Richard Gaskin wrote:
> Hugh Senior wrote:
>> Have you needed to sort lists by a value list or synchronize different 
>> lists, but thought it not easily do-able in Rev? You may find the 
>> following tips useful.
>> In a field, type some lines where the first word is a random day of 
>> the week, then...
>> on mouseUp
>>   sort lines of fld 1 by valueList(word 1 of each)
>> end mouseUp
>> function valueList what
>>   put "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday" into 
>> tList
>>   return itemOffset(what,tList)
>> end valueList
> I've been looking at this for two days and I can't wrap my head around it.
> My understanding of the sort command is that I can tell it to sort by a 
> particular chunk, and in which direction (ascending or descending), and 
> by what form of data (numeric or date or text).
> Indeed, the only examples in the docs are:
>    sort field "Output"
>    sort items of myInfo by word 2 of each -- sort by word 2 of the line
>    sort lines of field thisField descending numeric by item x of each
> I've never before seen a sort where the sort specifier is a literal 
> value. :\
> If I simplify the above to move the function result inline, it would 
> look like:
>   sort lines of fld 1 by "Monday"
> What exactly does that do, and how does it do it?
> And where did you learn how do that?  I can't find anything in the docs 
> like that.

It's just a custom sort function. Rev evaluates the function before 
sorting each line, so what it is actually sorting by is a number in this 
case. "Each" evaluates to the first word of each line in this case, 
which the function then re-evaluates as the position in the list of days 
of the week. So when "each" is "monday" the sort number is 1. When it's 
Tuesday, the sort number is 2, and so forth. I've posted some other 
custom sorts before. Actually, I think I once posted a more generic 
variation on Brett's script:

local lKeyData, lLineCounter

on sortBy keyField -- parallel sorting of linked flds
   put fld keyField into lKeyData
   put "data1,data2,data3" into dataFields -- fill in your fld names here
   repeat with i = 1 to the number of items of dataFields
     put 0 into lLineCounter
     sort lines of fld (item i of dataFields) by key()
   end repeat
end sortBy

function key
   add 1 to lLineCounter
   return line lLineCounter of lKeyData
end key

Same idea as the weekdays script, only this one is even more unweildy.

Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com

More information about the use-livecode mailing list