SORT Call

CALL SORT (matrix <, by> <, descend> ) ;

The SORT subroutine sorts a matrix by the values of one or more columns.

The arguments to the SORT subroutine are as follows:

matrix

is the input matrix. It is sorted in place by the call. If you want to preserve the original order of the data, make a copy of matrix.

by

specifies the columns used to sort the matrix. The argument by is either a numeric matrix that contains column numbers, or a character matrix that contains the names of columns assigned to matrix by a MATTRIB statement or READ statement. If by is not specified, then the first column is used.

descend

specifies which columns, if any, should be sorted in descending order. Any by columns not specified as descending will be ascending. If descend = by, then all by columns will be descending; if descend is skipped or is a null matrix, then all by columns will be ascending.

The SORT subroutine is used to sort a matrix according to the values in the columns specified by the by and descend arguments. Because the sort is done in place, very little additional memory space is required. The SORT subroutine is not as fast as the SORTNDX call for matrices with a large number of rows. After a matrix has been sorted, the unique combinations of values in the by columns can be obtained from the UNIQUEBY function.

For example, the following statements sort a matrix:

m = { 1 1 0,
      2 2 0,
      1 1 1,
      2 2 2};
call sort(m, {1 3}, 3); /* ascending by col 1; descending by col 3 */
print m;

Figure 23.323: Sorted Matrix

m
1 1 1
1 1 0
2 2 2
2 2 0