sortrows

Class: dataset

Sort rows of dataset array

The dataset data type might be removed in a future release. To work with heterogeneous data, use the MATLAB® table data type instead. See MATLAB table documentation for more information.

Syntax

B = sortrows(A)
B = sortrows(A,vars)
B = sortrows(A,'obsnames')
B = sortrows(A,vars,mode)
[B,idx] = sortrows(A)

Description

B = sortrows(A) returns a copy of the dataset array A, with the observations sorted in ascending order by all of the variables in A. The observations in B are sorted first by the first variable, next by the second variable, and so on. Each variable in A must be a valid input to sort, or, if a variable has multiple columns, to the MATLAB sortrows function or to its ownsortrows method.

B = sortrows(A,vars) sorts the observations in A by the variables specified by vars. vars is a positive integer, a vector of positive integers, variable names, a cell array containing one or more variable names, or a logical vector.

B = sortrows(A,'obsnames') sorts the observations in A by observation name.

B = sortrows(A,vars,mode) sorts in the direction specified by mode. When mode is the single string 'ascend' (the default) or 'descend', sortrows sorts A by the variables specified by vars in ascending or descending order, respectively. mode can also be a cell array containing the strings 'ascend' or 'descend', to specify a different sorting direction for each variable in vars. Specify [] for vars to sort using all variables.

[B,idx] = sortrows(A) also returns an index vector idx such that B = A(idx,:).

Examples

Sort the data in hospital.mat by age and then by last name:

load hospital
hospital(1:5,1:3)
ans = 
            LastName       Sex       Age
 YPL-320    'SMITH'        Male      38 
 GLI-532    'JOHNSON'      Male      43 
 PNI-258    'WILLIAMS'     Female    38 
 MIJ-579    'JONES'        Female    40 
 XLK-030    'BROWN'        Female    49 

hospital = sortrows(hospital,{'Age','LastName'});
hospital(1:5,1:3)
ans = 
            LastName       Sex       Age
 REV-997    'ALEXANDER'    Male      25 
 FZR-250    'HALL'         Male      25 
 LIM-480    'HILL'         Female    25 
 XUE-826    'JACKSON'      Male      25 
 SCQ-914    'JAMES'        Male      25 

Sort the data in hospital by gender in ascending order, and age in descending order.

hospital = sortrows(hospital,{'Sex','Age'},{'ascend','descend'});
hospital(1:5,1:3)
ans = 

               LastName        Sex       Age
    XLK-030    'BROWN'         Female    49 
    GGU-691    'HUGHES'        Female    49 
    KKL-155    'ADAMS'         Female    48 
    HQO-561    'BRYANT'        Female    48 
    BKD-785    'CLARK'         Female    48 

hospital(end-4:end,1:3)
ans = 

               LastName           Sex     Age
    VNL-702    'MOORE'            Male    28 
    REV-997    'ALEXANDER'        Male    25 
    FZR-250    'HALL'             Male    25 
    XUE-826    'JACKSON'          Male    25 
    SCQ-914    'JAMES'            Male    25 
Was this topic helpful?