NATSORTROWS Examples

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

To sort filenames, foldernames, or filepaths use NATSORTFILES.

To sort the elements of a string/cell array use NATSORT.

Contents

Basic Usage:

By default NATSORTROWS interprets consecutive digits as being part of a single integer, any remaining substring/s are treated as text.

A = {'A2','X';'A10','Y';'A10','X';'A1','X'}
sortrows(A) % Wrong number order
natsortrows(A) % Correct number order
A = 
    'A2'     'X'
    'A10'    'Y'
    'A10'    'X'
    'A1'     'X'
ans = 
    'A1'     'X'
    'A10'    'X'
    'A10'    'Y'
    'A2'     'X'
ans = 
    'A1'     'X'
    'A2'     'X'
    'A10'    'X'
    'A10'    'Y'

Input 1: Array to Sort

The first input must be one of the following array types:

The sorted array is returned as the first output argument. The input array must be a matrix (i.e. two dimensions only).

Input 2: Regular Expression

The optional second input argument is a regular expression which specifies the number matching (see "Regular Expressions" section below):

B = {'1.3','X';'1.10','X';'1.2','X'}
natsortrows(B) % By default match integers only.
natsortrows(B, '\d+\.?\d*') % Match decimal fractions.
B = 
    '1.3'     'X'
    '1.10'    'X'
    '1.2'     'X'
ans = 
    '1.2'     'X'
    '1.3'     'X'
    '1.10'    'X'
ans = 
    '1.10'    'X'
    '1.2'     'X'
    '1.3'     'X'

Input 3+: SORTROWS Column Argument

SORTROWS col input is supported, 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 descending, and the first ascending:

sortrows(A, [-2,1]) % Wrong number order.
natsortrows(A, [], [-2,1]) % Correct number order.
ans = 
    'A10'    'Y'
    'A1'     'X'
    'A10'    'X'
    'A2'     'X'
ans = 
    'A10'    'Y'
    'A1'     'X'
    'A2'     'X'
    'A10'    'X'

Inputs 3+: Optional Arguments

Further inputs are passed directly to NATSORT, thus giving control over the case sensitivity, sort direction, and other options. See the NATSORT help for explanations and examples of the supported options:

C = {'B','X';'10','X';'1','X';'A','X';'2','X'}
natsortrows(C, [], 'descend')
natsortrows(C, [], 'char<num')
C = 
    'B'     'X'
    '10'    'X'
    '1'     'X'
    'A'     'X'
    '2'     'X'
ans = 
    'B'     'X'
    'A'     'X'
    '10'    'X'
    '2'     'X'
    '1'     'X'
ans = 
    'A'     'X'
    'B'     'X'
    '1'     'X'
    '2'     'X'
    '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):

D = {'abc2xyz','Y';'abc10xyz','X';'abc2xyz','X';'abc1xyz','X'}
[out,ndx] = natsortrows(D)
D = 
    'abc2xyz'     'Y'
    'abc10xyz'    'X'
    'abc2xyz'     'X'
    'abc1xyz'     'X'
out = 
    'abc1xyz'     'X'
    'abc2xyz'     'X'
    'abc2xyz'     'Y'
    'abc10xyz'    'X'
ndx =
     4
     3
     1
     2

Output 3: Debugging Array

The third output is a cell vector of cell arrays, where the cell arrays correspond to the columns of the input cell array X. The cell arrays contain all matched numbers (after converting to numeric using the specified SSCANF format) and all split character substrings. These cell arrays are useful for confirming that the numbers are being correctly identified by the regular expression.

[~,~,dbg] = natsortrows(D);
dbg{:}
ans = 
    'abc'    [ 2]    'xyz'
    'abc'    [10]    'xyz'
    'abc'    [ 2]    'xyz'
    'abc'    [ 1]    'xyz'
ans = 
    'Y'
    'X'
    'X'
    'X'

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

NATSORTROWS number matching can be customized to detect numbers with a decimal fraction, E-notation, a +/- sign, binary/hexadecimal, or other required features. The number matching is specified using an appropriate regular expression, see NATSORT for details and examples.

E = {'v10.2','b'; 'v2.5','b'; 'v2.40','a'; 'v1.9','b'}
natsortrows(E) % Match integers, e.g. version numbers.
natsortrows(E,'\d+\.?\d*') % Match decimal fractions.
E = 
    'v10.2'    'b'
    'v2.5'     'b'
    'v2.40'    'a'
    'v1.9'     'b'
ans = 
    'v1.9'     'b'
    'v2.5'     'b'
    'v2.40'    'a'
    'v10.2'    'b'
ans = 
    'v1.9'     'b'
    'v2.40'    'a'
    'v2.5'     'b'
    'v10.2'    'b'

Bonus: Interactive Regular Expression Tool

Regular expressions are powerful and compact, but getting them right is not always easy. One assistance is to download my interactive tool IREGEXP, which lets you quickly try different regular expressions and see all of REGEXP's outputs displayed and updated as you type.