- Synopsis
**SORT**(`sequence`

**SORT**`sequence`

)`predicate`

- Description
Sorts a sequence (list, array, or word) in ascending order, or according to a given predicate. The output is a new sequence of the same type as

that contains the same items ordered such that there are no two successive items, a and b, where`sequence`

is FALSE and`(`

a b)`predicate`

is TRUE.`(`

b a)`predicate`

The

input must be a procedure that inputs two values and outputs either TRUE or FALSE.`predicate`

If no

input is given, then the predicate used depends on the`predicate`

input, as defined by the following table.`sequence`

type`sequence`

output A list of numbers A list sorted by LESSP A list of non-numbers A list sorted by BEFOREP An array of numbers An array sorted by LESSP An array of non-numbers An array sorted by BEFOREP A word A word whose characters are sorted by BEFOREP - Example
Sorting in ascending order:

[1 2 3]`SHOW SORT [3 1 2]`

Sorting in descending order:

[3 2 1]`SHOW (SORT [3 1 2] "GREATERP)`

Sorting complex records:

[[Dan 12 52] [Ana 12 52] [Erin 11 54] [Bob 11 54] [Cole 12 57]]`; [name age height] MAKE "a [Ana 12 52] MAKE "b [Bob 11 54] MAKE "c [Cole 12 57] MAKE "d [Dan 12 52] MAKE "e [Erin 11 54] MAKE "data (LIST :e :d :c :b :a) ; Sort by height. SHOW (SORT :data [LESSP LAST ?1 LAST ?2])`

[[Ana 12 52] [Dan 12 52] [Bob 11 54] [Erin 11 54] [Cole 12 57]]`; Sort by height with ties sorted by name. SHOW (SORT (SORT :data [BEFOREP FIRST ?1 FIRST ?2]) [LESSP LAST ?1 LAST ?2])`