sort

Synopsis

set sort(set)
tset sort(tset, int)

Description

The sort command can be used to sort a set or a relation. In its first form, it takes a set as its sole argument, and returns a set identical to the argument except that all elements are sorted in ascending order. If the set contains only numbers, numeric comparisons are used for sorting. If the set contains both numeric and non-numeric entities (or non-numeric entities only), string comparisons are used.

It should be noted that in general sets and relations are defined as having no ordering, and it is therefore unwise to rely upon a set or relation being sorted in data processing. Sorting is best used for presentation purposes only.

The second form of the command can be used to sort a relation. The second parameter (an integer) must be given to indicate the column of the relation on which the sorting should be performed. The numbering of relation columns is zero-based.

Examples

Here we create a simple set and sort it. Note how the behaviour of the sort built-in changes once a non-numeric member is added to the target set:

>> someSet = {21,11,1,12,2}
>> someSet
1
2
11
12
21
>> sort(someSet)
1
2
11
12
21
>> someSet = someSet + {"1 hello"}
>> sort (someSet)
1
1 hello
11
12
2
21
Here is an example of sorting a relation first by its first column (number zero) and then by its second column (number one):

>> map = {1} X {"one"} + {2} X {"two"} + {3} X {"three"}
>> sort (map,0)
1 one
2 two
3 three
>> sort (map, 1)
1 one
3 three
2 two