REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 3-Aug-2010 Edit History  

REBOL 3 Functions: sort

sort  series  /case  /skip  size  /compare  comparator  /part  length  /all  /reverse

Sorts a series. (Modifies)

Arguments:

series [series!]

Refinements:

/case - Case sensitive sort

/skip - Treat the series as records of fixed size

size [integer!] - Size of each record

/compare - Comparator offset, block or function

comparator [integer! block! any-function!]

/part - Sort only part of a series

length [number! series!] - Length of series to sort

/all - Compare all fields

/reverse - Reverse sort order

See also:

append   change   clear   insert   remove  

Description

Sorting will modify any type of series passed as the argument:

blk: [799 34 12 934 -24 0]
sort blk
print blk
-24 0 12 34 799 934
print sort "dbeca"
"abcde"

Normally sorting is not sensitive to character cases:

sort ["Fred" "fred" "FRED"]
["fred" "FRED" "Fred"]

But you can make it sensitive with the /CASE refinement:

sort/case ["Fred" "fred" "FRED"]
["FRED" "Fred" "fred"]

Editor note: Sort bug here causes camel-case strings to be sorted incorrectly.

When using the /SKIP refinement, you can treat the series as a set of records of a fixed size. Here we sort by a "name" column, while "age" is skipped:

name-ages: [
    "Larry" 45
    "Curly" 50
    "Mo" 42
]
print sort/skip name-ages 2
Curly 50 Larry 45 Mo 42

A /COMPARE function can be specified to perform the comparison. This allows you to change the ordering of the SORT:

names: [
    "Larry"
    "Curly"
    "Mo"
]
print sort/compare names func [a b] [a < b]
Curly Larry Mo

The /ALL refinement will force the entire record to be passed as a series to the compare function. This is useful if you need to compare one or more fields of a record while also doing a skip operation.

Editor note: Need a good working example. This may not be possible until remaining SORT bugs are fixed.

When sorting pair! data (points and area sizes), the y coordinate is dominant. This is preferred to support the y sorting used by various graphics algorithms.

probe sort [1x2 2x1 0x0 1x0 0x1 1x1]
[0x0 1x0 0x1 1x1 2x1 1x2]


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin