MATLAB Examples

NATSORTROWS Examples

The function NATSORTROWS sorts the rows of a cell array of strings, taking into account any number values within the strings. This is known as a "natural order sort" or an "alphanumeric sort". Note that MATLAB's inbuilt SORTROWS function only sorts by character order.

For sorting filenames or filepaths use NATSORTFILES.

For sorting a cell array of strings use NATSORT.

Contents

Basic Usage:

By default NATSORTROWS interprets consecutive digits as being part of a single integer, each number is considered to be as wide as one letter:

A = {'B','2','X';'A','100','X';'B','10','X';'A','2','Y';'A','20','X'};
sortrows(A)
natsortrows(A)
ans = 
    'A'    '100'    'X'
    'A'    '2'      'Y'
    'A'    '20'     'X'
    'B'    '10'     'X'
    'B'    '2'      'X'
ans = 
    'A'    '2'      'Y'
    'A'    '20'     'X'
    'A'    '100'    'X'
    'B'    '2'      'X'
    'B'    '10'     'X'

Output 2: Sort Index

The second output argument is a numeric array of the sort indices ndx, such that Y = X(ndx,:) where for Y = natsortrows(X):

[~,ndx] = natsortrows(A)
ndx =
     4
     5
     2
     1
     3

Sort Direction: 'ascend' or 'descend' Argument

The second input argument may be either 'ascend' or 'descend', and all columns will be sorted accordingly:

natsortrows(A,'ascend')
natsortrows(A,'descend')
ans = 
    'A'    '2'      'Y'
    'A'    '20'     'X'
    'A'    '100'    'X'
    'B'    '2'      'X'
    'B'    '10'     'X'
ans = 
    'B'    '10'     'X'
    'B'    '2'      'X'
    'A'    '100'    'X'
    'A'    '20'     'X'
    'A'    '2'      'Y'

Sort Direction: SORTROWS Column Argument

The second input argument may be a numeric vector of column indices, exactly as per MATLAB's SORTROWS, where a positive integer will sort the corresponding column in ascending order, and a negative integer will sort the corresponding column in descending order. In this example the second column is sorted asending, and the third descending:

sortrows(A,[2,-3]) % wrong numeric order:
natsortrows(A,[2,-3]) % correct numeric order:
ans = 
    'B'    '10'     'X'
    'A'    '100'    'X'
    'A'    '2'      'Y'
    'B'    '2'      'X'
    'A'    '20'     'X'
ans = 
    'A'    '2'      'Y'
    'B'    '2'      'X'
    'B'    '10'     'X'
    'A'    '20'     'X'
    'A'    '100'    'X'

Regular Expression: Decimal Numbers, E-notation, +/- Sign.

NATSORTROWS is a wrapper for NATSORT, which means all of NATSORT's options are also supported. In particular the number recognition can be customized to detect numbers with decimal digits, E-notation, a +/- sign, or other specific features. This detection is defined by providing an appropriate regular expression: see NATSORT for details and examples.

G = {'v10.2','b'; 'v1.9','b'; 'v2.5','b'; 'v2.40','a'};
natsortrows(G,'descend') % integers
natsortrows(G,'descend','\d+\.\d+')
ans = 
    'v10.2'    'b'
    'v2.40'    'a'
    'v2.5'     'b'
    'v1.9'     'b'
ans = 
    'v10.2'    'b'
    'v2.5'     'b'
    'v2.40'    'a'
    'v1.9'     'b'