Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Sorting Blocks

 [1/7] from: fergus4::bellatlantic::net at: 29-Jan-2006 0:50


How do you sort a block of blocks on an element other than the first? For example: db: [ ["bob" "Smith" "12"] ["dan" "murphy" "25"] ["mike" "Coogan" "6"] ] How do I sort by last name or by age

 [2/7] from: AJMartin:orcon at: 29-Jan-2006 9:38


> How do you sort a block of blocks on an element other than the first? > For example:
<<quoted lines omitted: 5>>
> How do I sort by last name or by age >> DB: ["Bob" "Smith" 12 "Dan" "Murphy" 25 "Mike" "Coogan" 6]
== ["Bob" "Smith" 12 "Dan" "Murphy" 25 "Mike" "Coogan" 6]
>> sort/skip/compare DB 3 2
== ["Mike" "Coogan" 6 "Dan" "Murphy" 25 "Bob" "Smith" 12]
>> sort/skip/compare DB 3 3
== ["Mike" "Coogan" 6 "Bob" "Smith" 12 "Dan" "Murphy" 25]
>> sort/skip/compare/reverse DB 3 3
== ["Dan" "Murphy" 25 "Bob" "Smith" 12 "Mike" "Coogan" 6]
>> help sort
USAGE: SORT series /case /skip size /compare comparator /part length /all /reverse DESCRIPTION: Sorts a series. SORT is an action value. ARGUMENTS: series -- (Type: series port) REFINEMENTS: /case -- Case sensitive sort. /skip -- Treat the series as records of fixed size. size -- Size of each record. (Type: integer) /compare -- Comparator offset, block or function. comparator -- (Type: integer block function) /part -- Sort only part of a series. length -- Length of series to sort. (Type: integer) /all -- Compare all fields /reverse -- Reverse sort order I hope that helps. AJ Martin Auto-surfing for 4% daily at: http://www.surfmunkee.com/?ref=7295 E-Gold: http://2255725.e-gold.com/

 [3/7] from: carl:cybercraft at: 29-Jan-2006 9:38


On Sunday, 29-January-2006 at 0:50:19 Alan Macleod wrote,
>How do you sort a block of blocks on an element other than the first? >For example:
<<quoted lines omitted: 4>>
> ] >How do I sort by last name or by age
Use SORT's COMPARE refinement. ie... sort/compare db func [a b][a/2 < b/2] sort/compare db func [a b][(to-integer a/3) < to-integer b/3] -- Carl Read.

 [4/7] from: AJMartin:orcon at: 29-Jan-2006 9:38


> How do you sort a block of blocks on an element other than the first?
To sort a block of blocks, use the /comparator thingy and pass a comparison function to the Sort function. AJ Martin Auto-surfing for 4% daily at: http://www.surfmunkee.com/?ref=7295 E-Gold: http://2255725.e-gold.com/

 [5/7] from: AJMartin:orcon at: 29-Jan-2006 9:38


>> How do you sort a block of blocks on an element other than the first? > > To sort a block of blocks, use the /comparator thingy and pass a > comparison function to the Sort function.
/compare thingy = refinement AJ Martin I know this! :) Auto-surfing for 4% daily at: http://www.surfmunkee.com/?ref=7295 E-Gold: http://2255725.e-gold.com/

 [6/7] from: anton:wilddsl:au at: 29-Jan-2006 18:06


The COMPARATOR argument can be an integer, which just specifies which field you want to be the sort key. Eg, to sort by second name:
>> sort/compare/reverse db 2
== [ ["bob" "Smith" "12"] ["dan" "murphy" "25"] ["mike" "Coogan" "6"] ] For more complex sorts, you pass your own function, but to make it a stable sort (doesn't change order when compared elements are the same), you need to return one of three values: -1, 0, or 1. Eg: sort/compare db func [a b][ case [a/2 > b/2 [1] a/2 < b/2 [-1] true 0] ] == [ ["mike" "Coogan" "6"] ["dan" "murphy" "25"] ["bob" "Smith" "12"] ] Regards, Anton.

 [7/7] from: fergus4:bellatlantic at: 29-Jan-2006 2:34


Never mind, I found a short function that does the job...from the library: File: %sort-nests.r Author: "Jeff Kreis" The function: sort/compare d func [a b][(pick a i) < pick b i] Where I is the column you wish to sort on.

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted