Using Sort to Rearrange the Contents of Each Line in a List
Mac OS 9 Mac OS X Windows Linux

> This command:
>
> sort lines of ... by blahBlahBlah
>
> gets a sort key by calling (virtually) this function on each line:
>
> function sortKey each
> return blahBlahBlah
> end if
>
> If I'm right, you should be able to do other interesting sorts.

Right, and it works great. You can write a custom sort function that does just about anything you can think of. Here's an example. I had a list of contact info exported as a comma-delimited list of itemized lines:
first,last,phone
jacque,gay,724-1596
bob,smith,123-1234
mary,jones,345-6929
john,doe,939-2939
And I wanted to re-arrange the contents of each line so that the last name was first, followed by the first name, then the phone. This did it:
local theCount,theRightOrder
on dosort
   put fld 1 into theData -- the original list
   put "2,1,3" into theRightOrder -- the key line
   repeat for each line l in theData
     put 0 into theCount
     sort items of l by mySort(each)
     put l & return after theNewData
   end repeat
   put theNewData into fld 2
end dosort

function mySort
   add 1 to theCount
   return item theCount of theRightOrder
end mySort
You could alter this to sort data in one field based on the order in another field, or to use any arbitrary order you want. Lots of variations are possible.

Posted on 1/14/03 by Jacqueline Landman Gay (HyperActive Software) to the Use-Revolution list   (See the complete post/thread)