## Natural-Order Row Sort

version 3.4.4 (23.8 KB) by
Alphanumeric row sort of a cell/string/categorical/table array, with customizable number format.

1.6K Downloads

Updated 31 Jan 2022

View License

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

To sort any file-names or folder-names use NATSORTFILES:
To sort the elements of a string/cell array use NATSORT:
Summary
Alphanumeric sort the rows of a string/cell/categorical/table array. Sorts the text by character code taking into account the values of any number substrings. Compare for example:
>> X = {'x2','10';'x10','0';'x1','0';'x2','2'};
>> sortrows(X) % Wrong numeric order
ans =
'x1' '0'
'x10' '0'
'x2' '10'
'x2' '2'
>> natsortrows(X) % Correct numeric order
ans =
'x1' '0'
'x2' '2'
'x2' '10'
'x10' '0'
By default NATSORTROWS interprets all consecutive digits as integer numbers, the number substring recognition can be specified using a regular expression: 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 supported by NATSORTROWS.
Examples
>> A = {'B','2','X';'A','100','X';'B','10','X';'A','2','Y';'A','20','X'};
>> sortrows(A) % SORTROWS for comparison.
ans =
'A' '100' 'X'
'A' '2' 'Y'
'A' '20' 'X'
'B' '10' 'X'
'B' '2' 'X'
>> natsortrows(A)
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'
>> sortrows(A,[2,-3]) % SORTROWS for comparison.
ans =
'B' '10' 'X'
'A' '100' 'X'
'A' '2' 'Y'
'B' '2' 'X'
'A' '20' 'X'
>> natsortrows(A,[],[2,-3])
ans =
'A' '2' 'Y'
'B' '2' 'X'
'B' '10' 'X'
'A' '20' 'X'
'A' '100' 'X'
>> natsortrows(A,[],[false,true,true],{'ascend','descend'})
ans =
'A' '2' 'Y'
'B' '2' 'X'
'B' '10' 'X'
'A' '20' 'X'
'A' '100' 'X'
>> natsortrows(A,[],{'ignore','ascend','descend'})
ans =
'A' '2' 'Y'
'B' '2' 'X'
'B' '10' 'X'
'A' '20' 'X'
'A' '100' 'X'
T = cell2table(A);
natsortrows(T,[], [2,-3]) % TABLE
ans =
A1 A2 A3
___ _____ ___
'A' '2' 'Y'
'B' '2' 'X'
'B' '10' 'X'
'A' '20' 'X'
'A' '100' 'X'
natsortrows(T,[], {'A2','A3'},{'ascend','descend'}) % TABLE
ans =
A1 A2 A3
___ _____ ___
'A' '2' 'Y'
'B' '2' 'X'
'B' '10' 'X'
'A' '20' 'X'
'A' '100' 'X'
>> B = {'ABCD';'3e45';'67.8';'+Inf';'-12';'+9';'NaN'};
>> sortrows(B) % SORTROWS for comparison.
ans =
'+9'
'+Inf'
'-12'
'3e45'
'67.8'
'ABCD'
'NaN'
>> natsortrows(B,'[-+]?(NaN|Inf|\d+\.?\d*(E[-+]?\d+)?)')
ans =
'-12'
'+9'
'67.8'
'3e45'
'+Inf'
'NaN'
'ABCD'
>> C = {'A2', 2; 'A10', 1; 'A2', 1}; % Scalar numerics in a column:
>> natsortrows(C,[],'sortnum')
ans =
'A2' [1]
'A2' [2]
'A10' [1]
>> natsortrows(C,[],'sortnum',{'ascend','descend'})
ans =
'A2' [2]
'A2' [1]
'A10' [1]

### Cite As

Stephen23 (2022). Natural-Order Row Sort (https://www.mathworks.com/matlabcentral/fileexchange/47433-natural-order-row-sort), MATLAB Central File Exchange. Retrieved .

##### MATLAB Release Compatibility
Created with R2010b
Compatible with R2009b and later releases
##### Platform Compatibility
Windows macOS Linux

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!