Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# sortrows

Class: ordinal

The nominal and ordinal array data types might be removed in a future release. To represent ordered and unordered discrete, nonnumeric data, use the MATLAB® categorical data type instead.

## Syntax

B = sortrows(A)
B = sortrows(A,col)
[B,I] = sortrows(A)
[B,I] = sortrows(A,col)

## Description

B = sortrows(A) sorts the rows of the 2-D ordinal matrix A in ascending order, as a group. B is an ordinal array with the same levels as A.

B = sortrows(A,col) sorts A based on the columns specified in the vector col. If an element of col is positive, the corresponding column in A is sorted in ascending order; if an element of col is negative, the corresponding column in A is sorted in descending order.

[B,I] = sortrows(A) and [B,I] = sortrows(A,col) also returns an index matrix I such that B = A(I,:).

Elements with undefined levels are sorted to the end.

## Examples

Sort the rows of an ordinal array in ascending order for the first column, and then in descending order for the second column:

```A = ordinal([6 2 5; 2 4 1; 3 2 4],...
{'lo','med','hi'},[],[0 2 4 6])
A =
hi       med      hi
med      hi       lo
med      med      hi

B = sortrows(A,[1 -2])
B =
med      hi       lo
med      med      hi
hi       med      hi ```