SORT [DESCENDING] inputVector resultVariable

If the keyword descending is absent, SORT replaces the contents of the result variable with the contents of the input vector sorted in ascending order. If the keyword descending is present, SORT replaces the contents of the result variable with the contents of the input vector sorted in descending order.

Missing data, represented by "NaN" or ".", is assumed to be larger than the largest value than can be expressed. Therefore NaNs will sort to the end of an ascending sort and to the beginning of a descending sort.

A vector to be sorted may contain named constants (constants created using the NAME command). There are two kinds of named constants. A named constant that is given a value by its NAME command is called a "named value". A named constant that is not given a value by its NAME command is called an "enum" (short for "enumerator"). All named constants created by one single NAME command are considered to be of the same "type". Named constants created by different NAME commands are considered to be of different types.

If your vector contains only enums, the elements will be sorted into the same order in which they appeared in their respective NAME commands. If the vector contains only named values, they will be sorted into order based on their assigned values, irrespective of their order in their NAME command.

A "mixed vector" is a vector that contains both numbers and named constants. If you SORT a mixed vector, the named constants will fall into place based on the rules just discussed. Specifically, enums will come first (assuming ascending order) and named values and numbers will be positioned according to their values.

See the second example at the right for a demonstration of the different ways in which the different types of elements are ordered.

Usage with the LET command

You can use SORT as a function in the LET command. By default, it will do an ascending sort. If you want to use it to do a descending sort, use the name SORT_DESCENDING, which is only valid within a LET command.


COPY (1 2 3 NaN 5 6 7 NaN) a
SORT a b
SORT descending a c
PRINT b c

The above program produced the following result:

b: (1.0 2.0 3.0 5.0 6.0 7.0 NaN NaN)
c: (NaN NaN 7.0 6.0 5.0 3.0 2.0 1.0)

The next example shows different ways that mixed vectors will sort depending on whether their named constants have assigned values or not.

NAME Jan Feb Mar               'Doesn't give values to these names
NAME 1,3 Ace Deuce Trey        'Gives values to these names
'Make 3 mixed vectors:
COPY 1,3 jan,mar vec1          'Numbers and names without values
COPY 1,3 Ace,Trey vec2         'Numbers and names with values
COPY 1,3 jan,mar Ace,Trey vec3 'Numbers and both kinds of names
'Sort vectors to show how the different
'types interact:
SORT vec1 vec1Sorted
SORT vec2 vec2Sorted
SORT vec3 vec3Sorted
PRINT table vec1 vec1Sorted
PRINT table vec2 vec2Sorted
PRINT table vec3 vec3Sorted

Here is the output of the above program with comments interspersed. The first table shows the result of sorting a vector that contains numbers, and names that don't have values associated with them ("enums"). The names get sorted to the top in the same order that they were declared in their NAME command.

vec1          	vec1Sorted    	
1             	Jan           	
2             	Feb           	
3             	Mar           	
Jan           	1             	
Feb           	2             	
Mar           	3             	

The second table shows the result of sorting a vector that contains numbers, and names that were given values by their NAME command (I.e., named values). Since the named values have values they are compared by value with the numbers.

vec2          	vec2Sorted    	
1             	1             	
2             	Ace           	
3             	2             	
Ace           	Deuce         	
Deuce         	3             	
Trey          	Trey          	

The third table shows the result of sorting a vector that contains numbers, enums, and named values.

vec3          	vec3Sorted    	
1             	Jan           	
2             	Feb           	
3             	Mar           	
Jan           	1             	
Feb           	Ace           	
Mar           	2             	
Ace           	Deuce         	
Deuce         	3             	
Trey          	Trey