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

[REBOL] Re: Sorting Blocks

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.