[TIP] Sorting by ValueList and synchronized sorting

classic Classic list List threaded Threaded
29 messages Options
12
Reply | Threaded
Open this post in threaded view
|

[TIP] Sorting by ValueList and synchronized sorting

Hugh Senior
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
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

Hope some of you may find the above helpful.


/H

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

_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

Phil Davis-5
Thanks Hugh! This is quite cool.
Phil Davis


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
>
> 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 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
>
> Hope some of you may find the above helpful.
>
>
> /H
>
> Hugh Senior
> www.ssBk.co.uk, Home of the Scripter's Scrapbook

--
Phil Davis

PDS Labs
Professional Software Development
http://pdslabs.net

_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

Richard Gaskin
In reply to this post by Hugh Senior
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 -

--
  Richard Gaskin
  Managing Editor, revJournal
  _______________________________________________________
  Rev tips, tutorials and more: http://www.revJournal.com
_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

J. Landman Gay
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         |     [hidden email]
HyperActive Software           |     http://www.hyperactivesw.com
_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

masmit
In reply to this post by Richard Gaskin
Richard, if you look at the 'valueList' function, it returns a number  
ie. the offset of a weekday name into the list of weekday names. It's  
this value that is being used to sort the list. It's like attaching a  
temporary value to each line and sorting by that.

If there were more than 9 weekdays, this example would have needed to  
specify numeric.

This is why you can sort by random(someBigNumber) to randomize a list  
- each line effectively gets a random number, and is sorted by it.

Best,

Mark

On 17 Jun 2008, at 22:49, 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.
>
> TIA -
>
> --
>  Richard Gaskin
>  Managing Editor, revJournal
>  _______________________________________________________
>  Rev tips, tutorials and more: http://www.revJournal.com
> _______________________________________________
> use-revolution mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your  
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution

_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

Jim Ault
In reply to this post by Richard Gaskin
On 6/17/08 2:49 PM, "Richard Gaskin" <[hidden email]> 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" <[hidden email]> 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 -


_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Firefox 3.0

Joe Lewis Wilkins
Hi all,

I'm a loyal Safari user, but I thought it would be a good idea to at  
least take a look at the new Firefox release. It downloaded quickly  
and installed without any problem. It looks to be really clean and  
fast. I'll report back later.

The download site is:    <http://www.spreadfirefox.com/>

Joe Wilkins
_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

Richard Gaskin
In reply to this post by Hugh Senior
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
  ___________________________________________________________
  [hidden email]       http://www.FourthWorld.com

_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

masmit

On 17 Jun 2008, at 23:57, Richard Gaskin wrote:
> 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

Except that though we don't know exactly what the engine is doing,  
it's probably computing the value of the sort expression for each  
line and then doing the sort.

Best,

Mark
_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

Richard Gaskin
In reply to this post by Hugh Senior
Mark Smith wrote the magic phrase that made this much clearer to me:

 > It's like attaching a temporary value to each line and sorting
 > by that.

The moment of "a ha!"  Thank you, Mark.  Jim Ault's detailed explanation
was also helpful.

So this tells me two things:

1. Using a function for a sortKey expression introduces a "sometimes"
rule in terms of understanding the order of expression evaluation in the
engine, in which most of the time functions are evaluated first but in
this case the function is applied repeatedly for each line of the sort
container as the sort command is run.

2. Using a function as a sortKey expression evaluates the sort container
  as though by effectively adding data to it, rather than anything
necessarily in the data itself.

Both of these are very valuable insights.  Thanks to all who helped
explain this.

Yes indeed, now I can see many areas where this can be useful, but it
also leaves me with a question:

Given #1 above, how does this affect performance?  Unless there's
something ultra-tricky going on (wouldn't be the first time the engine
surprised me that way <g>), I would imagine that performance is affected
at least linearly, in which the overhead of the function call is
multiplied by the number of lines of the container to arrive at the
additional performance hit relative to a non-custom sort.

Perhaps I'll do some benchmarking to verify this theory....

--
  Richard Gaskin
  Managing Editor, revJournal
  _______________________________________________________
  Rev tips, tutorials and more: http://www.revJournal.com
_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

J. Landman Gay
In reply to this post by Richard Gaskin
Richard Gaskin wrote:

> But I guess I'm stuck on the old (outdated?) notion that functions are
> evaluated before their result is used in the calling command,

It's still doing that. The function is evaluated first, before the sort
assigns it a position. The difference here is that "each" is evaluated
for each element that is being sorted; the function runs once per line.


> I'm certain that you're not crazy,

You may be taking too much for granted here. :)

--
Jacqueline Landman Gay         |     [hidden email]
HyperActive Software           |     http://www.hyperactivesw.com
_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

J. Landman Gay
In reply to this post by Richard Gaskin
Richard Gaskin wrote:
>
> 1. Using a function for a sortKey expression introduces a "sometimes"
> rule in terms of understanding the order of expression evaluation in the
> engine, in which most of the time functions are evaluated first but in
> this case the function is applied repeatedly for each line of the sort
> container as the sort command is run.

Well, not really. It's more like a substitution for what the engine does
anyway. When the engine sorts a container, it has to assign a position
for each element being sorted. A custom sort function does the same
thing; the engine will use the number returned by the function when
arranging the elements.

The order of evaluation hasn't changed, and functions are still
evaluated first. But when sorting, each element has to be evaluated
individually.


>
> 2. Using a function as a sortKey expression evaluates the sort container
>  as though by effectively adding data to it, rather than anything
> necessarily in the data itself.

I guess you could think of it that way, but the paradigm seems a little
skewed to me. Sorting a container requires the engine to assign a
position to each element. If a custom sort function assigns a position
instead, the engine will use the number returned by the function.
Internally the engine is actually still assigning positions, but because
the function provides a straightforward numeric order, the engine
doesn't have to figure out what the order is, it just uses the numbers.

I don't think I'm explaining this very well. ;)

>
> Given #1 above, how does this affect performance?

In my experience, not much. I suppose with a long list you might see a
difference though.

>  Unless there's
> something ultra-tricky going on (wouldn't be the first time the engine
> surprised me that way <g>), I would imagine that performance is affected
> at least linearly, in which the overhead of the function call is
> multiplied by the number of lines of the container to arrive at the
> additional performance hit relative to a non-custom sort.
>
> Perhaps I'll do some benchmarking to verify this theory....
>

Let us know.

--
Jacqueline Landman Gay         |     [hidden email]
HyperActive Software           |     http://www.hyperactivesw.com
_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

Richard Gaskin
In reply to this post by Hugh Senior
Richard Gaskin wrote:

> So this tells me two things:
>
> 1. Using a function for a sortKey expression introduces a "sometimes"
> rule in terms of understanding the order of expression evaluation in the
> engine, in which most of the time functions are evaluated first but in
> this case the function is applied repeatedly for each line of the sort
> container as the sort command is run.
>
> 2. Using a function as a sortKey expression evaluates the sort container
>   as though by effectively adding data to it, rather than anything
> necessarily in the data itself.
>
> Both of these are very valuable insights.  Thanks to all who helped
> explain this.
>
> Yes indeed, now I can see many areas where this can be useful, but it
> also leaves me with a question:
>
> Given #1 above, how does this affect performance?  Unless there's
> something ultra-tricky going on (wouldn't be the first time the engine
> surprised me that way <g>), I would imagine that performance is affected
> at least linearly, in which the overhead of the function call is
> multiplied by the number of lines of the container to arrive at the
> additional performance hit relative to a non-custom sort.
>
> Perhaps I'll do some benchmarking to verify this theory....

FWIW, I ran some benchmarks and it turns out the theory seems to hold
up, in which the time taken for a custom function sort is roughly the
same as the time to call the function multiplied by the number of lines
of the data being sorted, plus the time of the sort itself.

This isn't surprising given how it works, but may be helpful if an
alternative algorithm for what you want to accomplish is available.

But given the grace of the syntax and the reasonably good performance of
the engine overall, if you need to use it you'll likely be hard-pressed
to come up with anything that does that which is faster.

For benchmarking fetishists, the code I tested is below.....

--
  Richard Gaskin
  Fourth World Media Corporation
  ___________________________________________________________
  [hidden email]       http://www.FourthWorld.com


on mouseUp
   put 1000 into n -- number of iterations to test
   put fld 1 into tData -- data to sort (mine was 535 lines)
   put the number of lines of tData into tLineNum -- for use in results
   --
   -- 1: Test time to call function
   put the millisecs into t
   repeat n
     get foo()
   end repeat
   put the millisecs - t into t1
   --
   -- 2: Test time to do straight sort
   put tData into tmp
   set the itemdel to tab
   put the millisecs into t
   repeat n
     sort lines of tmp by item 2 of each
   end repeat
   put the millisecs - t into t2
   --
   -- 3: Time to do custom sort:
   put tData into tmp
   put the millisecs into t
   repeat n
     sort lines of tmp by foo()
   end repeat
   put the millisecs - t into t3
   --
   -- Results:
   put "For "&n&" iterations - "&\
       cr&"Time to call foo: "&t1  &\
       cr&"Time to perform simple sort: "&t2 &\
       cr&"Time to perform foo sort: "& t3 &\
       cr&"(foo time * number of lines)+ simple sort time = "&\
         (t1* tLineNum)+ t2
end mouseUp



-- I tested with two variants of foo, each taking different approaches
-- with very different performance times:

--- Serious time-waster:
function foo
   repeat 100
     get random(9999)
   end repeat
   return it
end foo

-- Just complex enough to be measurable at 1000 iterations:
function foo
   get random(9999)
   add 44 to it
   subtract 4444 from it
   put 1* it into it
   put it div 5 + 6 into it
   get it + it / 4
   get round(it) + 10 + 1 + 1
   return it
end foo

_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

Richard Gaskin
In reply to this post by Hugh Senior
J. Landman Gay wrote:

> Richard Gaskin wrote:
>>
>> 1. Using a function for a sortKey expression introduces a "sometimes"
>> rule in terms of understanding the order of expression evaluation in the
>> engine, in which most of the time functions are evaluated first but in
>> this case the function is applied repeatedly for each line of the sort
>> container as the sort command is run.
>
> Well, not really. It's more like a substitution for what the engine does
> anyway. When the engine sorts a container, it has to assign a position
> for each element being sorted. A custom sort function does the same
> thing; the engine will use the number returned by the function when
> arranging the elements.
>
> The order of evaluation hasn't changed, and functions are still
> evaluated first. But when sorting, each element has to be evaluated
> individually.

Right.  That's what initially threw me.

As far as I can recall, this optional form of the sort command is the
only way to call a function iteratively without using a repeat loop.

Are there others I've forgotten?

It's very handy, but a cognitive hurdle for me at first.  I'd never seen
anything like it before.

Anyone else have a mental hiccup when they first encountered this?

--
  Richard Gaskin
  Managing Editor, revJournal
  _______________________________________________________
  Rev tips, tutorials and more: http://www.revJournal.com
_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

J. Landman Gay
Richard Gaskin wrote:

> It's very handy, but a cognitive hurdle for me at first.  I'd never seen
> anything like it before.

You probably blocked it. :)

<http://search.gmane.org/?query=custom+sort&group=gmane.comp.ide.revolution.user>

>
> Anyone else have a mental hiccup when they first encountered this?
>

All of us. The concept is really snarly, and requires some mental
gymnastics. And even with the ones I've written myself, I still have to
figure them out all over again each time I want to use them.

Speaking of mental gymnastics, here's my favorite. I use it a lot. I
didn't write it, it was written by Tony Root from HyperPro back in the
day, and turned into a function by Paul Foraker:

function whichOne var,fld1,fld2  -- from a handler by Tony Root
-- Handles a case where you need to return one value if your key is
empty, another if not.

   return (item offset(char 1 of (var = empty),"tf") of quote & fld1
&","& fld2 & quote)
end whichOne

Paul says:

"There's a great scripter working at Digital Pictures named Tony Root,
who wrote a one-line boolean evaluator that I corrupted into a function.
This handles the case where you need to return one value if your key is
empty, another if not. For example, if the social security number field
(var) is empty, then return "Please fill in your social security number"
(fld1); otherwise, return "Now please fill in the form on the next
page." (fld2). The elegant part (which Tony wrote) is getting the offset
of char 1 of an expression that resolves to true or false, of "tf". Wish
I'd thought of that. "

You don't have to use it for just empty/not empty. With some
adjustments, you can use it to choose between any two values.

--
Jacqueline Landman Gay         |     [hidden email]
HyperActive Software           |     http://www.hyperactivesw.com
_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

Richard Gaskin
In reply to this post by Hugh Senior
J. Landman Gay wrote:

> Richard Gaskin wrote:
>> It's very handy, but a cognitive hurdle for me at first.  I'd never seen
>> anything like it before.
>
> You probably blocked it. :)
> <http://search.gmane.org/?query=custom+sort&group=gmane.comp.ide.revolution.user>

Wow.  So many posts I've overlooked.  Must have had stuff in beta during
those threads. :)

Glad I took the time today, thought.  You _can_ teach an old dog new
tricks. :)


> Speaking of mental gymnastics, here's my favorite. I use it a lot. I
> didn't write it, it was written by Tony Root from HyperPro back in the
> day, and turned into a function by Paul Foraker:
>
> function whichOne var,fld1,fld2  -- from a handler by Tony Root
> -- Handles a case where you need to return one value if your key is
> empty, another if not.
>
>    return (item offset(char 1 of (var = empty),"tf") of quote & fld1
> &","& fld2 & quote)
> end whichOne
>
> Paul says:
>
> "There's a great scripter working at Digital Pictures named Tony Root,
> who wrote a one-line boolean evaluator that I corrupted into a function.
> This handles the case where you need to return one value if your key is
> empty, another if not. For example, if the social security number field
> (var) is empty, then return "Please fill in your social security number"
> (fld1); otherwise, return "Now please fill in the form on the next
> page." (fld2). The elegant part (which Tony wrote) is getting the offset
> of char 1 of an expression that resolves to true or false, of "tf". Wish
> I'd thought of that. "
>
> You don't have to use it for just empty/not empty. With some
> adjustments, you can use it to choose between any two values.

In other languages that's called iff.  Paul's/Tony's implementation is
very clever, but I believe it benchmarks a bit faster (and is easier to
read) when written as an if-else without the itemoffset and concatenation:

function whichOne pVal, pTrue, pFalse
    if pVal is empty then return pTrue
    else return pFalse
end whichOne


A more classic iff would evaluate any expression to true or false - this
one should handle empties as false as well:

function iff pExp, pTrue, pFalse
   if pExp is true then return pTrue
   else return pFalse
end iff

--
  Richard Gaskin
  Managing Editor, revJournal
  _______________________________________________________
  Rev tips, tutorials and more: http://www.revJournal.com
_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

J. Landman Gay
Richard Gaskin wrote:

> In other languages that's called iff.

Whoa, I didn't know there was a name for it. Makes sense, since Tony
came to HyperCard from other languages. So he was probably translating.

> Paul's/Tony's implementation is
> very clever, but I believe it benchmarks a bit faster (and is easier to
> read) when written as an if-else without the itemoffset and concatenation:

Yeah, I'm sure you're right.

For some reason that reminds me of my New York friend, who wouldn't
signal her lane changes. When we asked her why, she said, "What, and
give away my strategy?"

--
Jacqueline Landman Gay         |     [hidden email]
HyperActive Software           |     http://www.hyperactivesw.com
_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

Jerry Jensen
In reply to this post by Hugh Senior
> From: Richard Gaskin <[hidden email]>
>
> But I guess I'm stuck on the old (outdated?) notion that functions are
> evaluated before their result is used in the calling command

The "AHA" moment for me was some time ago when Jacque shared the  
beautiful line to shuffle something:

sort <something> by random()

AHA!

Cheer,
Jerry Jensen

P.S. Randomness is next to Godliness...

_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

J. Landman Gay
In reply to this post by Richard Gaskin
But wait, there's more! Some time ago when the list was discussing
custom parallel sorts, Dick Kriesel offered this enhancement:

  split varB by comma
  sort items of varA by varB[itemOffset(each,varA)]

If Dick's still reading the list, he should stand up and take a bow.

--
Jacqueline Landman Gay         |     [hidden email]
HyperActive Software           |     http://www.hyperactivesw.com
_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
Reply | Threaded
Open this post in threaded view
|

Re: [TIP] Sorting by ValueList and synchronized sorting

Hugh Senior
In reply to this post by Hugh Senior
This is very elegant. With a little more tweaking, you could use it to
choose any of more than 2 values! Now in the Scripter's Scrapbook.

Thank you, Jacque.

/H


J. Landman Gay wrote:
function whichOne var,fld1,fld2  -- from a handler by Tony Root
-- Handles a case where you need to return one value if your key is
empty, another if not.

   return (item offset(char 1 of (var = empty),"tf") of quote & fld1
&","& fld2 & quote)
end whichOne

Paul says:

"There's a great scripter working at Digital Pictures named Tony Root,
who wrote a one-line boolean evaluator that I corrupted into a function.
This handles the case where you need to return one value if your key is
empty, another if not. For example, if the social security number field
(var) is empty, then return "Please fill in your social security number"
(fld1); otherwise, return "Now please fill in the form on the next
page." (fld2). The elegant part (which Tony wrote) is getting the offset
of char 1 of an expression that resolves to true or false, of "tf". Wish
I'd thought of that. "

You don't have to use it for just empty/not empty. With some
adjustments, you can use it to choose between any two values.

_______________________________________________
use-revolution mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution
12