File Exchange

image thumbnail

Natural-Order Row Sort

version 1.6.0.0 (12.2 KB) by Stephen Cobeldick
Natural-order row sort of a cell array of strings, with customizable numeric format.

12 Downloads

Updated 25 Mar 2018

View License

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 (1xN char). 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 = {'-12';'ABCD';'3e45';'67.8';'+9';'+Inf';'NaN'};
sortrows(B)
ans =
'+9'
'+Inf'
'-12'
'3e45'
'67.8'
'ABCD'
'NaN'
natsortrows(B,[],'NaN|(+|-)?(Inf|\d+\.?\d*((e|E)(+|-)?\d+)?)')
ans =
'-12'
'+9'
'67.8'
'3e45'
'+Inf'
'NaN'
'ABCD'

Comments and Ratings (4)

Siwaphon

thank you very much, It's helpful

This is brilliant!
Thank you!

Matt H

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

Updates

1.6.0.0

* Add (very useful) debugging output argument.

1.5.0.0

* Improve blurb and HTML.

1.5.0.0

* Minor help edit

1.5.0.0

* Add HTML documentation.

1.5.0.0

* Improve input checking.
* include NATSORT function.

1.4.0.0

* Fix missing image.

1.4.0.0

* Include screenshot.

1.4.0.0

* Clearer description of file dependency.

1.3.0.0

* Improve function description.
* Better examples.

1.2.0.0

- Update documentation only, improve examples.

1.1.0.0

- Complete acknowledgements.

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor