sortrows

  • sortrows(X) sorts rows of X in ascending order
  • sortrows(X, cols) sorts X based on columns specified in cols
  • sortrows(X, -cols) sorts X based on columns specified in cols in descending order
  • [Y, ix] = sortrows(X) with ix satisfying Y = X(ix)

Examples

> X = ['baac';'bccc';'dbcc';'aacc']#

            baac
            bccc
            dbcc
            aacc

> sortrows(X, [4 -1])#

            dbcc
            baac
            bccc
            aacc

> [Y, index] = sortrows(X, 2)

> Y#

            baac
            aacc
            dbcc
            bccc

> index#

            1
            4
            3
            2
> ID = {'A1';'A2';'A3';'A4'}; Gender = {'M';'F';'F';'F'}; Age =[15;10;11;9]; T1= table(ID, Gender, Age)

            [ID:4x1 cell]
            [Gender:4x1 cell]
            [Age:4x1 double]
            [T1:4x3 table]

> [T2, index] = sortrows(T1, {'Age', 'Gender'})


> T2#

            ID	Gender	Age
            A4	F	9
            A2	F	10
            A3	F	11
            A1	M	15

Resources

See also

sort | unique