SORTROWS

Purpose:

Sorts the rows of an array based on column numbers.

Syntax:

SORTROWS(a, cols, order)

(v, idx) = SORTROWS(a, cols, order)

a

-

A series or array.

cols

-

Optional an integer or series, the column numbers. If a column value is negative, the sort order for that column is descending. Defaults to all columns.

order

-

Optional. An integer or string, the sort direction.

0 or "descend"

:

descending order

1 or "ascend"

:

ascending order (default)

Returns:

A sorted series or array.

 

(v, idx) = SORT(a, cols, order) returns the sorted array and the indices of the sorted values in separate series.

Example:

W1: {{5, 9, 4},

     {2, 0, 6},

     {2, 9, 8},

     {5, 6, 1}}

W2: sortrows(w1)

 

W2 contains the sorted array:

 

{{2, 0, 6},

 {2, 9, 8},

 {5, 6, 1},

 {5, 9, 4}}

Example:

W1: {{5, 9, 4},

     {2, 0, 6},

     {2, 9, 8},

     {5, 6, 1}}

W2: (v, idx) = sortrows(w1);W1[idx, ..]

 

Same as above, the array in W1 is sorted based on the returned indices:

 

{{2, 0, 6},

 {2, 9, 8},

 {5, 6, 1},

 {5, 9, 4}}

Example:

W1: {{5, 9, 4},

     {2, 0, 6},

     {2, 9, 8},

     {5, 6, 1}}

W2: sortrows(w1, 2)

W3: sortrows(w1, -2)

 

The array in W1 is sorted based on column 2.

 

W2 sorts in ascending order:

 

{{2, 0, 6},

 {5, 6, 1},

 {5, 9, 4},

 {2, 9, 9}}

 

W3 sorts in descending order:

 

{{5, 9, 4},

 {2, 9, 8},

 {5, 6, 1},

 {2, 0, 6}}

Example:

W1: {{5, 9, 4},

     {2, 0, 6},

     {2, 9, 8},

     {5, 6, 1}}

W2: sortrows(w1, 2, "ascend")

W3: sortrows(w1, 2, "descend")

 

Same as above.

 

W2 sorts in ascending order:

 

{{2, 0, 6},

 {5, 6, 1},

 {5, 9, 4},

 {2, 9, 9}}

 

W3 sorts in descending order:

 

{{5, 9, 4},

 {2, 9, 8},

 {5, 6, 1},

 {2, 0, 6}}

Example:

W1: {{5, 9, 4},

     {2, 0, 6},

     {2, 9, 8},

     {5, 6, 1}}

W2: sortrows(w1, {3, 2}

 

W2 sorts the array in W1 based on column 3 and column 2:

 

{{5, 6, 1},

 {5, 9, 4},

 {2, 0, 6},

 {2, 9, 8}}

Example:

W1: {{1 + 1i,   100},

     {0 + 0.5i, 200},

     {3 - 4i,   300}}

W2: sortrows(w1)

 

W2 sorts the complex array in W1:

 

{{0 + 0.5i, 200 + 0i},

 {1 + 1i,   100 + 0i},

 {3 - 4i,   300 + 0i}}

Remarks:

SORTROWS performs a stable sort such that the original order of duplicate values is preserved.

 

Complex values are sorted by magnitude first. If the magnitudes of the compared values are equal, the values are sorted on phase from -π to π. If the imaginary parts of the compared complex values are 0, the real part (including sign) instead of the magnitude is the first sort criterion.

 

 Note, unlike SORT, the default order for SORTROWS is 1 or "ascend".

 

See SORT for more details on sorting.

See Also:

CPLXPAIR

DTSORT

GRADE

LOOKUP

MEDIAN

REORDER

SETBUFSIZE

SORT

STRSORT