sortrows

Sort array rows

Syntax

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

Description

example

B = sortrows(A) sorts the rows of A in ascending order. For strings, this is the familiar dictionary sort.

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.

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.

example

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

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 string, 'ascend', indicates ascending order (default) and 'descend' indicates descending order.

example

tblB = sortrows(tblA,vars,mode) uses mode to specify the sort order. mode can be 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

Start with an arbitrary matrix, A.

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

Sort Rows of Cell Array

Create a 6-by-2 cell array of strings.

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

    '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 = 

    '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 names in the second column in descending order.

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

    '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 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 it sorts 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 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

expand all

A — Input arraycolumn vector | matrix

Input array, specified as a column vector or matrix. The data type of A can be numeric, logical, char, cell, categorical, datetime, or duration. 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 [−π, π].

Complex Number Support: Yes

column — Column sorting vectorvector of integers

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.

tblA — Input tabletable

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

Data Types: table

'RowNames' — Row sort inputstring

Row sort input, specified as the string '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.

vars — Sorting variablesinteger | vector of integers | variable name | cell array of variable names | logical vector

Sorting variables, specified as an 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.

mode — Sorting modesingle string | cell array of strings

Sorting mode, specified as a single string or cell array of strings composed of the options 'ascend' (default), or 'descend'. If mode is a cell array of strings, 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

expand all

B — Sorted arrayarray

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

tblB — Sorted tabletable

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

index — Sort indexindex vector

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

See Also

|

Was this topic helpful?