[TIP] Sorting by ValueList and synchronized sorting

Richard Gaskin ambassador at fourthworld.com
Tue Jun 17 18:57:23 EDT 2008


J. Landman Gay wrote:

 > Richard Gaskin wrote:> on mouseUp
 >>> 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
 >>
 >> 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?
...
 > 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.

Yes, I was mistaken in thinking it returned "Monday"; it wasn't using 
"item itemoffset(", but merely "itemoffset(" so it was returning "1" 
rather than "Monday".  So far so good.

But I guess I'm stuck on the old (outdated?) notion that functions are 
evaluated before their result is used in the calling command, which 
would mean the calling sort command is effectively using:

    sort lines of fld 1 by 1

1 what?

My tired brain still isn't getting it. :\


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

Only to your nimble mind.  To my addled one its workings remain a mystery.

I'm certain that you're not crazy, and uncertain about myself in that 
regard, so I'm confident once I wrap my head around this it will be very 
useful....

-- 
  Richard Gaskin
  Fourth World Media Corporation
  ___________________________________________________________
  Ambassador at FourthWorld.com       http://www.FourthWorld.com




More information about the use-livecode mailing list