sort()

Synopsis

Returns a copy of the input list , sorted according to dir. The direction dir must be either :Ascending or :Descending. The list must be homogeneous. All elements within list must be either numeric ( Integers and/or Numbers ) or alphabetic ( Strings ), unless an appropriate key function is supplied. An error is signaled if a heterogeneous list is supplied.

Note that secondary (and tertiary) sorts are also possible. If two elements are equal (after applying the key function) there is a mechanism to sort them by secondary criteria. The "sort order" and "key function" arguments can be a list of Names . The first order/key is the primary sorting criterion. Additional criteria are used as required.

Syntax

sort ( list As List, _
       dir As Name, _
       Optional key As Name = :Identity) As List 
Argument Type Description
list List The input list to be sorted.
dir Any Specifies the sort direction -- can be either :Ascending or :Descending. Can be specified as either a single value, which will be used for all sort keys, or as a list of values -- one for each sort key -- that defines specific sort direction for each key.
key Any Optional; specifies the name of a function to be applied to each of the items in the list to get a "key" to be used for sorting; default is :Identity. The key function must return either a numeric or string value, and you can specify a list of function names to perform a multi-level sort.

Example 1

Using no sort key
Intent >sort({:a, :d, :r, :c, :b}, :Descending) 
--> {:r, :d, :c, :b, :a} 
Returns a simple list of Name values, sorted in reverse order of their string values.

Example 2

Error: using a heterogeneous list
Intent >sort({:a, :d, :r, 3.1, :b, :c}, :Ascending) 
Error: Heterogeneous list (after processing with 'key' function #1).

The key used for sorting must be homogenous.

Example 3

Error: Default sort key not applicable
Intent >sort({{:a,:d}, {:r,:b}, {:c,:x}}, :Ascending) 
Error: Key function #1 did not return an Integer , Number , String or Name .

To sort a list of items other than numbers or strings , you must specify the name of a key function that returns either a number or a string for every item in the list . In this example, attempting to sort a list of lists directly raised an error.

Example 4

Using a key function
Intent >sort({{:a,:d}, {:r,:b}, {:c,:x}}, :Ascending, key := :first) 
--> {{:a, :d}, {:c, :x}, {:r, :b}} 
The lists in this example were sorted using the function first as the key.

Example 5

Using a different key function
Intent >sort({{:a,:d}, {:r,:b}, {:c,:x}}, :Ascending, key := :second) 
--> {{:r, :b}, {:a, :d}, {:c, :x}} 
These lists were sorted using the function second as the key.

Example 6

Using 2 sort keys
 Intent >sort({{4,:a,7}, {1,:c,4}, {4,:a,5}, {1,:c,3}, {1,:a,5}}, :Ascending, key := {:first, :third}) 
--> {{1, :c, 3}, {1, :c, 4}, {1, :a, 5}, {4, :a, 5}, {4, :a, 7}}
The lists in this example were sorted using the function first as the primary key and the function third as a secondary key.

Example 7

Using 3 sort keys
 Intent >sort({{4,:a,7}, {1,:c,4}, {4,:a,5}, {1,:c,3}, {1,:a,5}}, :Ascending, key := {:first, :second, :third}) 
--> {{1, :a, 5}, {1, :c, 3}, {1, :c, 4}, {4, :a, 5}, {4, :a, 7}}
The lists in this example were sorted using the function first as the primary key, the function second as the secondary key, and the function third as the tertiary key. Each of the sort keys was applied in ascending order.

Example 8

Using different sort orders per key
 Intent >sort({{4,:a,7}, {1,:c,4}, {4,:a,5}, {1,:c,3}, {1,:a,5}}, {:ascending, :descending}, key := {:first, :third}) 
--> {{1, :a, 5}, {1, :c, 4}, {1, :c, 3}, {4, :a, 7}, {4, :a, 5}} 
The lists in this example were sorted using the function first as the primary key and the function third as a secondary key - but the sort directions are ascending and descending respectively. If all the sort criteria have the same direction, you don't have to duplicate it - see Examples 6 and 7 above.