File Exchange

## Natural-Order Row Sort

version 1.5 (11.1 KB) by

Natural-order row sort of a cell array of strings, with customizable numeric format.

Updated

Editor's Note: This file was selected as MATLAB Central Pick of the Week

To sort filenames or filepaths use NATSORTFILES:
http://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort
To sort all of the strings in a cell array use NATSORT:
http://www.mathworks.com/matlabcentral/fileexchange/34464-customizable-natural-order-sort

### Summary ###

Alphanumeric sort of the rows of a cell array of strings. Similar to MATLAB's SORTROWS function, but takes into account the values of any numeric substrings occurring within the strings.

By default NATSORTROWS simply treats all consecutive digits as integer values, however number recognition can be controlled by a regular expression: this allows decimal digits, +/- sign, exponent, binary, octal, or hexadecimal notation, and more. There are also options for controlling the sort direction and case sensitivity. See NATSORT for details.

### File Dependency ###

The natural-order sort is provided by the function NATSORT (File Exchange 34464). All of NATSORT's optional inputs are also supported by NATSORTROWS, eg: to define a regular expression that matches the numeric substrings, select case sensitivity, etc. The <col> input of MATLAB's SORTROWS is also supported, so you can use NATSORTROWS as a drop-in replacement for SORTROWS.

### Examples ###

A = {'B','2','X';'A','100','X';'B','10','X';'A','2','Y';'A','20','X'};
sortrows(A) % wrong numeric order:
ans =
'A','100','X'
'A', '2','Y'
'A', '20','X'
'B', '10','X'
'B', '2','X'
natsortrows(A) % correct numeric order:
ans =
'A', '2','Y'
'A', '20','X'
'A','100','X'
'B', '2','X'
'B', '10','X'
natsortrows(A,'descend')
ans =
'B', '10','X'
'B', '2','X'
'A','100','X'
'A', '20','X'
'A', '2','Y'
% Sort ascending by the second column, descending by the third column:
sortrows(A,[2,-3]) % wrong numeric order:
ans =
'B', '10','X'
'A','100','X'
'A', '2','Y'
'B', '2','X'
'A', '20','X'
natsortrows(A,[2,-3]) % correct numeric order:
ans =
'A', '2','Y'
'B', '2','X'
'B', '10','X'
'A', '20','X'
'A','100','X'

B = {'a','-12';'a','ABCD';'a','3e45';'a','67.8';'a','9';'a','+Inf';'a','NaN'};
sortrows(B)
ans =
'a','+Inf'
'a','-12'
'a','3e45'
'a','67.8'
'a','9'
'a','ABCD'
'a','NaN'
natsortrows(B,'ascend','NaN|(+|-)?(Inf|\d+(\.\d+)?((e|E)(+|-)?\d+)?)')
ans =
'a','-12'
'a','9'
'a','67.8'
'a','3e45'
'a','+Inf'
'a','NaN'
'a','ABCD'

C = {'A',10;'a','XX';'A',2;'a',1;'A','#'}; % mixed numeric and string data
D = cellfun(@num2str,C,'UniformOutput',false); % convert all to string
natsortrows(D)
ans =
'A','#'
'a','1'
'A','2'
'A','10'
'a','XX'
natsortrows(D,'ascend',[],'matchcase','asdigit') % see NATSORT for options list
ans =
'A','#'
'A','2'
'A','10'
'a','1'
'a','XX'
natsortrows(D,'ascend',[],'beforechar') % see NATSORT for options list
ans =
'a','1'
'A','2'
'A','10'
'A','#'
'a','XX'

Chang hsiung

Matt H

### Matt H (view profile)

Thank you very much for this, it saved me a lot of time.

FYI at first I ran into some trouble with natsortrows saying my array must be a cell array of strings, then I tried sortrows which cleared things up by saying some cells in X contain non-scalar values. I had some empty cells left over in my array after running textscan. I was able to get it working using the following if it helps anyone else:

isArrayEmpty = cellfun('isempty',cellArray);
cellArray(isArrayEmpty) = {''};
cellArray = natsortrows(cellArray);

Thanks again