# Documentation

### This is machine translation

Translated by
Mouse over text to see original. Click the button below to return to the English verison of the page.

# sortrows

Sort rows of array, table, or timetable

## Syntax

• tblB = sortrows(tblA)
example
• tblB = sortrows(tblA,'RowNames')
example
• tblB = sortrows(tblA,rowTimesName)
• tblB = sortrows(tblA,vars)
example
• tblB = sortrows(tblA,mode)
example
• tblB = sortrows(tblA,'RowNames',mode)
example
• tblB = sortrows(tblA,rowTimesName,mode)
• tblB = sortrows(tblA,vars,mode)
example
• [tblB,index] = sortrows(tblA,___)
example

## Description

example

B = sortrows(A) sorts the rows of A in ascending order of the first column. Character arrays are sorted in the familiar dictionary order, which uses ASCII values. When the first column has equal values, sortrows sorts according to the next column and repeats this behavior for succeeding equal values.

example

B = sortrows(A,column) sorts matrix A based on the columns specified in the vector, column. This input is used to perform multiple column sorts in succession. For equal values, the original order is preserved.

example

[B,index] = sortrows(___) also returns an index vector using any of the previous syntaxes. The index vector satisfies B = A(index,:).

example

tblB = sortrows(tblA) sorts the rows of table tblA in ascending order by the first variable, then by the second variable, and so on.

If tblA is a timetable, then sortrows sorts the rows of tblA in ascending order by its row times.

example

tblB = sortrows(tblA,'RowNames') sorts by the row names.

If tblA is a timetable, then sortrows(tblA,'RowNames') is the equivalent of sortrows(tblA).

tblB = sortrows(tblA,rowTimesName) sorts by the row times when tblA is a timetable and has a time vector with the name specified by rowTimesName.

example

tblB = sortrows(tblA,vars) sorts by the variables specified by vars.

example

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

tblB = sortrows(tblA,rowTimesName,mode) sorts tblA in the order specified by mode when tblA is a timetable and has a time vector with the name specified by rowTimesName.

example

tblB = sortrows(tblA,vars,mode) uses mode to specify the sort order. mode can be a character vector or a cell array of character vectors containing 'ascend' for ascending order (default) or 'descend' for descending order.

• When mode is a character vector, sortrows sorts in the specified direction for all variables in vars.

• When mode is a cell array of character vectors, 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

collapse all

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

Sort the rows of A.

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 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).

When called with two input arguments, sortrows bases the sort entirely on the column specified in the second argument.

Sort the rows of A based on the values in the second column.

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

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.

Specify two columns to sort by: columns 1 and 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

sortrows sorts by column 1 first, and then for any rows with equal values in column 1, sorts by column 7.

Sort the rows in descending order using the values in column 4.

[E,index] = 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

index =

2
5
1
4
3
6

The index vector, index, describes the rearrangement of the rows, such that E = A(index,:).

Create a 6-by-2 cell array of character vectors.

A = {'Germany' 'Lukas'; 'USA' 'William'; 'USA' 'Andrew'; ...
'Germany' 'Andreas'; 'USA' 'Olivia'; 'Germany' 'Julia'}
A =

6×2 cell array

'Germany'    'Lukas'
'USA'        'William'
'USA'        'Andrew'
'Germany'    'Andreas'
'USA'        'Olivia'
'Germany'    'Julia'

The result is a list of countries and names.

Sort the rows of A.

B = sortrows(A)
B =

6×2 cell array

'Germany'    'Andreas'
'Germany'    'Julia'
'Germany'    'Lukas'
'USA'        'Andrew'
'USA'        'Olivia'
'USA'        'William'

The result is an alphabetized list sorted by both country and name.

Sort the countries first, then sort the names in descending order.

[C,index] = sortrows(A,[1 -2])
C =

6×2 cell array

'Germany'    'Lukas'
'Germany'    'Julia'
'Germany'    'Andreas'
'USA'        'William'
'USA'        'Olivia'
'USA'        'Andrew'

index =

1
6
4
2
5
3

The index vector, index, describes the rearrangement of the rows, such that C = A(index,:).

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 it sorts by the variable Height.

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.

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 sort in 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

## Input Arguments

collapse all

Input array, specified as a column vector or matrix. If A contains NaN values or undefined categorical or datetime elements, sortrows(A) places them on the high end of the sort (as if they are large numbers).

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

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | cell | categorical | datetime | duration
Complex Number Support: Yes

Column sorting vector, specified as a vector of integers. Each integer value indicates a column to sort by. The sign of the integer indicates ascending (positive) or descending (negative) sort order.

Example: sortrows(A,[2 -3]) sorts the rows of A first in ascending order for the second column, and then it sorts by descending order for the third column.

Input table, specified as a table or a timetable. Each variable in tblA must be a valid input to sort or sortrows.

Data Types: table | timetable

Row sort input, specified as the character vector 'RowNames'. Specify the 'RowNames' option to sort a table by row names rather than by variables. If tblA.Properties.RowNames is empty, sortrows(tblA,'RowNames') returns tblA.

Data Types: char

Name of the time vector of a timetable, specified as a character vector. The vector that contains row times is a property of a timetable. This property has a name you can specify when you create a timetable, such as Time or Date.

Example: If the timetable, TT, has a time vector named Date, then sortrows(TT,'Date') sorts the timetable on the dates and times that Date contains.

Data Types: char

Sorting variables, specified as a scalar integer, a vector of integers, a variable name, a cell array of variable names, or a logical vector. vars indicates the table variables to sort by. You also can use the mode input to indicate ascending or descending order for each sorting variable.

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 the integers.

Example: sortrows(tblA,{'Height','Weight'}) sorts the rows of tblA in ascending order, first by the variable Height, and then it sorts by the variable Weight.

Example: sortrows(tblA,[1 4],{'descend' 'ascend'}) sorts the first variable of tblA in descending order, then it sorts the fourth variable in ascending order.

Data Types: double | single | char | cell | logical

Sorting mode, specified as a character vector or cell array of character vectors composed of the options 'ascend' (default), or 'descend'. If mode is a cell array of character vectors, the number of required entries depends on whether you are sorting by variables or by row names.

• If tblA is being sorted by variables, the cell array must have an entry for each variable.

• If tblA is being sorted by row names, the cell array must have one entry.

Data Types: char | cell

## Output Arguments

collapse all

Sorted array, returned as an array of the same size and class as A.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | cell | categorical | datetime | duration

Sorted table, returned as a table or a timetable with the same variables as tblA.

Data Types: table | timetable

Sort index, returned as an index vector. The sort index describes the rearrangement of elements or rows in the input.

Data Types: double