Accelerating the pace of engineering and science

Documentation Center

• Trial Software

sortrows

Sort rows in ascending order

Syntax

• B = sortrows(A) example
• B = sortrows(A,column) example
• [B,index] = sortrows(A,...)
• tblB = sortrows(tblA) example
• tblB = sortrows(tblA,'RowNames') example
• tblB = sortrows(tblA,vars)
• tblB = sortrows(tblA,mode)
• tblB = sortrows(tblA,'RowNames',mode)
• tblB = sortrows(tblA,vars,mode)
• [tblB,index] = sortrows(tblA,...) example

Description

example

B = sortrows(A) sorts the rows of A in ascending order. Argument A must be either a matrix or a column vector.

For strings, this is the familiar dictionary sort.

When A is complex, sortrows sorts the elements by magnitude, and, where magnitudes are equal, further sorts by phase angle on the interval [−π, π].

example

B = sortrows(A,column) sorts the matrix based on the columns specified in the vector, column. If an element of column is positive, MATLAB® sorts the corresponding column of matrix A in ascending order. If an element of column is negative, MATLAB sorts the corresponding column in descending order. For example, sortrows(A,[2 -3]) sorts the rows of A first in ascending order for the second column, and then by descending order for the third column.

[B,index] = sortrows(A,...) also returns an index vector index.

If A is a column vector, then B = A(index). If A is an m-by-n matrix, then B = A(index,:).

example

tblB = sortrows(tblA) sorts the rows of a table, tblA, in ascending order first by the first variable, next by the second variable, and so on. Each variable in tblA must be a valid input to sort or sortrows.

example

tblB = sortrows(tblA,'RowNames') sorts the rows of tblA by the row names. If tblA.Properties.RowNames is empty, sortrows(tblA,'RowNames') returns tblA.

tblB = sortrows(tblA,vars) sorts by the variables specified by vars. You can specify the input argument, vars, as an integer, a vector of integers, a variable name, a cell array containing one or more variable names, or a logical vector.

If an element of vars is a positive integer, sortrows sorts the corresponding variable in tblA in ascending order. If an element of vars is a negative integer, sortrows sorts the corresponding variable in tblA in descending order. If you provide the mode input argument, MATLAB ignores the sign of an integer.

tblB = sortrows(tblA,mode) and tblB = sortrows(tblA,'RowNames',mode) sorts tblA in the direction specified by mode. The single string, 'ascend', indicates ascending order (default) and 'descend' indicates descending order.

tblB = sortrows(tblA,vars,mode) sorts tblA in the direction specified by mode. You can specify mode as a single string or a cell array of strings containing 'ascend' for ascending order (default) or 'descend' for descending order.

When mode is a single string, sortrows sorts in the specified direction for all variables in vars.

When mode is a cell array of strings, sortrows sorts in the specified direction for each variable in vars.

example

[tblB,index] = sortrows(tblA,...) also returns an index vector, index, such that tblB = tblA(index,:).

Examples

expand all

Sort Rows of Matrix

```A = floor(gallery('uniformdata',[6 7],0)*100);
A(1:4,1) = 95;  A(5:6,1) = 76;  A(2:4,2) = 7;  A(3,3) = 73```
```A =
95    45    92    41    13     1    84
95     7    73    89    20    74    52
95     7    73     5    19    44    20
95     7    40    35    60    93    67
76    61    93    81    27    46    83
76    79    91     0    19    41     1```

When called with only a single input argument, sortrows bases the sort on the first column of the matrix. For any rows that have equal elements in a particular column, (e.g., A(1:4,1) for this matrix), sorting is based on the column immediately to the right, (A(1:4,2) in this case):

`B = sortrows(A)`
```B =
76    61    93    81    27    46    83
76    79    91     0    19    41     1
95     7    40    35    60    93    67
95     7    73     5    19    44    20
95     7    73    89    20    74    52
95    45    92    41    13     1    84```

When called with two input arguments, sortrows bases the sort entirely on the column specified in the second argument. Rows that have equal elements in the specified column, (e.g., A(2:4,:), if sorting matrix A by column 2) remain in their original order:

`C = sortrows(A,2)`
```C =
95     7    73    89    20    74    52
95     7    73     5    19    44    20
95     7    40    35    60    93    67
95    45    92    41    13     1    84
76    61    93    81    27    46    83
76    79    91     0    19    41     1
```

This example specifies two columns to sort by: columns 1 and 7. This tells sortrows to sort by column 1 first, and then for any rows with equal values in column 1, to sort by column 7:

`D = sortrows(A,[1 7])`
```D =
76    79    91     0    19    41     1
76    61    93    81    27    46    83
95     7    73     5    19    44    20
95     7    73    89    20    74    52
95     7    40    35    60    93    67
95    45    92    41    13     1    84```

Sort the matrix using the values in column 4 this time and in reverse order:

`E = sortrows(A, -4)`
```E =
95     7    73    89    20    74    52
76    61    93    81    27    46    83
95    45    92    41    13     1    84
95     7    40    35    60    93    67
95     7    73     5    19    44    20
76    79    91     0    19    41     1```

Sort Rows of Table

Sort the rows of a table by the variable values in ascending order.

Create a table with four variables listing patient information for five people.

```LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];

tblA = table(Age,Height,Weight,BloodPressure,'RowNames',LastName)```
```tblA =

Age    Height    Weight     BloodPressure
---    ------    ------    ---------------
Smith       38     71        176       124          93
Johnson     43     69        163       109          77
Williams    38     64        131       125          83
Jones       40     67        133       117          75
Brown       49     64        119       122          80```

Sort the rows of the table.

`tblB = sortrows(tblA)`
```tblB =

Age    Height    Weight     BloodPressure
---    ------    ------    ---------------
Williams    38     64        131       125          83
Smith       38     71        176       124          93
Jones       40     67        133       117          75
Johnson     43     69        163       109          77
Brown       49     64        119       122          80
```

The sortrows function sorts the rows in ascending order first by the variable Age, and then by the variable Height.

Sort Rows of Table By Row Names

Create a table with four variables listing patient information for five people.

```LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];

tblA = table(Age,Height,Weight,BloodPressure,'RowNames',LastName)```
```tblA =

Age    Height    Weight     BloodPressure
---    ------    ------    ---------------
Smith       38     71        176       124          93
Johnson     43     69        163       109          77
Williams    38     64        131       125          83
Jones       40     67        133       117          75
Brown       49     64        119       122          80```

Sort the rows of the table by the row names and return an index vector such that tblB = tblA(index,:).

`[tblB,index] = sortrows(tblA,'RowNames')`
```tblB =

Age    Height    Weight     BloodPressure
---    ------    ------    ---------------
Brown       49     64        119       122          80
Johnson     43     69        163       109          77
Jones       40     67        133       117          75
Smith       38     71        176       124          93
Williams    38     64        131       125          83

index =

5
2
4
1
3```

The sortrows function sorts the rows in ascending order by the row names.

Sort Rows of Table By Variables

Create a table with four variables listing patient information for five people.

```LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];

tblA = table(Age,Height,Weight,BloodPressure,'RowNames',LastName)```
```tblA =

Age    Height    Weight     BloodPressure
---    ------    ------    ---------------
Smith       38     71        176       124          93
Johnson     43     69        163       109          77
Williams    38     64        131       125          83
Jones       40     67        133       117          75
Brown       49     64        119       122          80```

Sort the rows of the table in ascending order by Height, and then descending order by Weight. Also, return an index vector such that tblB = tblA(index,:).

`[tblB,index] = sortrows(tblA,{'Height','Weight'},{'ascend','descend'})`
```tblB =

Age    Height    Weight     BloodPressure
---    ------    ------    ---------------
Williams    38     64        131       125          83
Brown       49     64        119       122          80
Jones       40     67        133       117          75
Johnson     43     69        163       109          77
Smith       38     71        176       124          93

index =

3
5
4
2
1```