table

Methods

Method Reference: table.sortrows

table: tblB = sortrows (tblA)
table: tblB = sortrows (tblA, 'RowNames')
table: tblB = sortrows (tblA, rowDimName)
table: tblB = sortrows (tblA, vars)
table: tblB = sortrows (tblA, …, direction)
table: tblB = sortrows (…, Name, Value)
table: [tblB, index] = sortrows (…)

Sort the rows of a table.

tblB = sortrows (tblA) sorts the rows in tblA in ascending order based on the values in the first variable. If elements in the first variable are repeated, then sortrows sorts by the elements in the second variable, and so on.

tblB = sortrows (tblA, 'RowNames') sorts the table tblA according to its row names. If tblA does not have row names, i.e. tblA.Properties.RowNames is empty, then it returns tblA.

tblB = sortrows (tblA, rowDimName) also sorts the table tblA along the first dimension, rowDimName, which is the equivalent to the previous syntax, i.e. according to its row names. If tblA does not have row names, that is tblA.Properties.RowNames is empty, then it returns tblA. For this syntax to work, rowDimName must match the first element in tblA.Properties.DimensionNames, otherwise rowDimName is considered a variable name, as in the following syntax.

tblB = sortrows (tblA, vars) sorts the rows in table tblA by the elements in the variables specified by vars, which can be a character vector (for a single variable) or a cell array of character vectors or a string array (specifying a single or multiple variables). If tblA has row names, then vars can include the row names. Alternatively, vars can be a logical vector or a numeric vector of real integers indexing the desired variables. Positive integers specify an ascending order, whereas negative integers specify a descending order for the referenced variables. You can also index all available variables in tblA by passing a semicolon character argument. This Octave-specific syntax facilitates the use of the direction input argument when no particular variable needs to be selected to sort on. Additionally, vars can be a vartype object used to create a subscript that selects variables of a specified type.

tblB = sortrows (tblA, …, direction) sorts the rows in table tblA in the order specified by direction for any of the previous syntaxes. direction can be 'ascend' or 'descend', which is applied to all specified variables or row names that sortrows operates on. direction can also be a cell array of character vectors, whose elements are 'ascend' and 'descend', where each element corresponds to the specified variables and/or row names used for sorting the table. The order specified by direction always takes precedence over the order defined by a numerical vector of integers in vars. direction must always be the 3rd input argument. If you want to omit passing selected variables and allow sortrows to work on consecutive variables until all ties are resolved, then you can leave the second input argument empty, as in sortrows (tblA, {[]}, direction) or pass a colon argument for vars as in sortrows (tblA, {':'}, direction).

tblB = sortrows (…, Name, Value) specifies additional parameters for sorting rows of a table with the following Name-Value paired arguments.

  • 'MissingPlacement' specifies the placement of missing values with one of the following options: 'auto' places the missing elements at the bottom for ascending order and at the top for descending order; 'first' places missing elements at the top; 'last' places missing elements at the bottom.
  • 'ComparisonMethod' specifies the element comparison method with one of the following options: 'auto' sorts rows using the real part for real numbers and the magnitude for complex numbers; 'real' sorts rows using the real part for both real and complex numbers; 'abs' sorts rows using the magnitude for both real and complex numbers. For complex numbers with equal magnitude, the phase angle in the interval (-π, π] is further used to break ties.

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

Source Code: table

Example: 1

sortrows orders the rows by one or more variables of any comparable type. Name the variables to sort by; a trailing direction (or one per key) flips ascending to descending. Here an ordinal categorical Grade sets the primary order, with a datetime breaking ties.

 Name = string ({'Li'; 'Diaz'; 'Brown'; 'Lee'});
 Visit = datetime (2024, [3; 1; 2; 1], [7; 5; 6; 2]);
 Grade = categorical ({'B'; 'A'; 'A'; 'B'}, {'A', 'B', 'C'}, 'Ordinal', true);
 T = table (Name, Visit, Grade)
T =
  4x3 table

     Name         Visit       Grade    
    _______    ___________    _____    

    "Li"       07-Mar-2024        B    
    "Diaz"     05-Jan-2024        A    
    "Brown"    06-Feb-2024        A    
    "Lee"      02-Jan-2024        B
 sortrows (T, {'Grade', 'Visit'})
ans =
  4x3 table

     Name         Visit       Grade    
    _______    ___________    _____    

    "Diaz"     05-Jan-2024        A    
    "Brown"    06-Feb-2024        A    
    "Lee"      02-Jan-2024        B    
    "Li"       07-Mar-2024        B

Sort the same table by Grade descending instead.

 sortrows (T, 'Grade', 'descend')
ans =
  4x3 table

     Name         Visit       Grade    
    _______    ___________    _____    

    "Li"       07-Mar-2024        B    
    "Lee"      02-Jan-2024        B    
    "Diaz"     05-Jan-2024        A    
    "Brown"    06-Feb-2024        A

Example: 2

A second output returns the permutation index, and passing 'RowNames' sorts by the row names themselves rather than by a data variable.

 Age = [38; 43; 38; 40];
 Smoker = logical ([1; 0; 1; 0]);
 T = table (Age, Smoker, 'RowNames', {'Sanchez', 'Johnson', 'Li', 'Diaz'})
T =
  4x2 table

               Age    Smoker    
               ___    ______    

    Sanchez     38    true      
    Johnson     43    false     
    Li          38    true      
    Diaz        40    false
 [S, idx] = sortrows (T, 'RowNames');
 S
S =
  4x2 table

               Age    Smoker    
               ___    ______    

    Diaz        40    false     
    Johnson     43    false     
    Li          38    true      
    Sanchez     38    true
 idx
idx =

   4
   2
   3
   1