NATSORTROWS Examples

The function NATSORTROWS sorts the rows of a cell array of strings (1xN char), 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 sorts only 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 = {'B1','X2';'A1','X100';'B1','X10';'A2','X2';'A1','X20';'A10','X1';'A2','X0'};
sortrows(A)
natsortrows(A)
ans = 
    'A1'     'X100'
    'A1'     'X20' 
    'A10'    'X1'  
    'A2'     'X0'  
    'A2'     'X2'  
    'B1'     'X10' 
    'B1'     'X2'  
ans = 
    'A1'     'X20' 
    'A1'     'X100'
    'A2'     'X0'  
    'A2'     'X2'  
    'A10'    'X1'  
    'B1'     'X2'  
    'B1'     'X10' 

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 =
     5
     2
     7
     4
     6
     1
     3

Output 3: Debugging Array

The third output is a cell vector of cell arrays, where each cell array contains individual characters and numbers (after converting to numeric). This is useful for confirming that the numbers are being correctly identified by the regular expression. The cells of the cell vector correspond to the columns of the original input cell matrix.

[~,~,dbg] = natsortrows(A);
dbg{:}
ans = 
    'B'    [ 1]
    'A'    [ 1]
    'B'    [ 1]
    'A'    [ 2]
    'A'    [ 1]
    'A'    [10]
    'A'    [ 2]
ans = 
    'X'    [  2]
    'X'    [100]
    'X'    [ 10]
    'X'    [  2]
    'X'    [ 20]
    'X'    [  1]
    'X'    [  0]

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 = 
    'A1'     'X20' 
    'A1'     'X100'
    'A2'     'X0'  
    'A2'     'X2'  
    'A10'    'X1'  
    'B1'     'X2'  
    'B1'     'X10' 
ans = 
    'B1'     'X10' 
    'B1'     'X2'  
    'A10'    'X1'  
    'A2'     'X2'  
    'A2'     'X0'  
    'A1'     'X100'
    'A1'     'X20' 

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 ascending, and the third descending:

sortrows(A,[-2,1]) % wrong numeric order:
natsortrows(A,[-2,1]) % correct numeric order:
ans = 
    'A1'     'X20' 
    'A2'     'X2'  
    'B1'     'X2'  
    'A1'     'X100'
    'B1'     'X10' 
    'A10'    'X1'  
    'A2'     'X0'  
ans = 
    'A1'     'X100'
    'A1'     'X20' 
    'B1'     'X10' 
    'A2'     'X2'  
    'B1'     'X2'  
    'A10'    'X1'  
    'A2'     'X0'  

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'; 'v2.5','b'; 'v2.40','a'; 'v1.9','b'};
natsortrows(G) % integers, e.g. version numbers
natsortrows(G,[],'\d+\.?\d*') % decimal values
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'

Regular Expression: 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.