[TIP] Sorting by ValueList and synchronized sorting

Hugh Senior FlexibleLearning at tiscali.co.uk
Sat Jun 14 14:25:35 EDT 2008

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

Of couse you could also apply any valid chunking expression such as (word 2 
to -1 of item 3 of each), apply the valueList sort to variables as well as 
fields, store multiple valueLists as re-usable custom properties, make the 
valueList conditional upon other factors and so on. The point is that the 
built-in sort keys can be extended to include your own preferential order.

Applying the same logic above to multiple fields (or indeed variables), the 
following implements parallel sorting of linked fields, keeping the lists 
synchronized (based on an old HyperCard script by Brett Sher)...

Create 3 fields called 'data1', 'data2' and 'data3'. Add lines of text into 
each field, for example a list of first names, last names, ages, then...

on mouseUp
  sortBy "data1" --| Sort by first name
end mouseUp

local lLineCounter,lKeyData
on sortBy keyField
  put fld keyField into lKeyData
  put "data1,data2,data3" into dataFields --| fill in your own field names 
  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

Hope some of you may find the above helpful.


Hugh Senior
www.ssBk.co.uk, Home of the Scripter's Scrapbook 

More information about the Use-livecode mailing list