[TIP] Sorting by ValueList and synchronized sorting

Jim Ault JimAultWins at yahoo.com
Tue Jun 17 18:43:00 EDT 2008


On 6/17/08 2:49 PM, "Richard Gaskin" <ambassador at fourthworld.com> wrote:
<full text of the question appears below>
> I've never before seen a sort where the sort specifier is a literal
> value. :\

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

Jeanne DeVoto wrote about this feature a few years ago.
The entry in the Rev docs for 'sort' describes "SortKey" as an *expression*
that evaluates to a value for each line.

This means that the expression could be a function call that returns a
result.

The idea is that Rev will generate a value for each line of a container.
This could be a chunk expression, or the use of a function that you write.
This value can be anything a function can return.

The 'each' key word says that considering each line, pass a param that is
equal to some string, and then run the function.

In most cases, your function will use this param to produce the desired
result, but does *not have to use the passed value* (see the first example
below).

The result returned by the function becomes the the value (or sort key) that
is used to re-order the lines of the container.  After it is used, it is
deleted.  This sort key does not become part of the line in the container.

----------
Three examples of generating a sort key


Make a new main stack
make one new field named "originalData"
put some data lines in to this field
paste the following script into the stack script
then Apply, 
then type "sortThis" into the message box, hit enter.

-------- start copy script  ----------
global gCurrLineCount

on sortThis
  get field "originalData"
  put 0 into gCurrLineCount
  sort lines of it descending numeric by numSortReverse(word 1 of each)
  put it into reverseSort
  get field "originalData"
  
  sort lines of it by numSortGrouping(word 2 of each)
  put it into birdsOfAFeatherSort
  
  get field "originalData"
  sort lines of it by distanceFromBeginning(word 1 of each)
  put it into distanceFromTopSort
  
  get reverseSort & cr & cr
  get it  & birdsOfAFeatherSort & cr & cr
  get it  & distanceFromTopSort
  
  put it into message box
end sortThis


function numSortReverse dummyVal
  -- we don't use the dummyVal in this case
  --  we don't care what the line contains
  add 1 to gCurrLineCount
  return (gCurrLineCount)
end numSortReverse


function numSortGrouping offsetStr
  -- birds of a feather sort
  put lineOffset(offsetStr, field "originalData") into position
  return position
end numSortGrouping

function distanceFromBeginning offsetStr
  put offset(offsetStr, field "originalData") into position
  return position
end distanceFromBeginning

-------------- end copy script --------------------

Hope this helps,

Jim Ault
Las Vegas


On 6/17/08 2:49 PM, "Richard Gaskin" <ambassador at fourthworld.com> 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.
> 
> TIA -





More information about the use-livecode mailing list