File Exchange

image thumbnail

Customizable Natural-Order Sort

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


Updated 26 Sep 2019

View License

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

To sort filenames or filepaths use NATSORTFILES:
To sort the rows of a cell array of strings use NATSORTROWS:

### Summary ###

Alphanumeric sort of a cell array of strings (1xN char). Sorts the strings taking into account the values of any number substrings occurring within those strings. Compare for example:

X = {'a2', 'a10', 'a1'};
ans = 'a1' 'a10' 'a2'
ans = 'a1' 'a2' 'a10'

By default NATSORT interprets all consecutive digits as integer numbers, the number substring recognition can be specified using a regular expression, allowing the number substrings to have:
* a +/- sign
* a decimal point and decimal fraction
* E-notation exponent
* decimal, octal, hexadecimal or binary notation
* Inf or NaN values
* criteria supported by regular expressions: look-arounds, quantifiers, etc.

And of course the sorting itself can also be controlled:
* ascending/descending sort direction
* character case sensitivity/insensitivity
* relative order of numbers vs. characters
* relative order of numbers vs NaNs

### Examples ###

%% Multiple integers (e.g. release version numbers):
>> A = {'v10.6', 'v9.10', 'v9.5', 'v10.10', 'v9.10.20', 'v9.10.8'};
>> sort(A)
ans = 'v10.10' 'v10.6' 'v9.10' 'v9.10.20' 'v9.10.8' 'v9.5'
>> natsort(A)
ans = 'v9.5' 'v9.10' 'v9.10.8' 'v9.10.20' 'v10.6' 'v10.10'

%% Integer, decimal, NaN, or Inf numbers, possibly with +/- signs:
>> B = {'test+NaN', 'test11.5', 'test-1.4', 'test', 'test-Inf', 'test+0.3'};
>> sort(B)
ans = 'test' 'test+0.3' 'test+NaN' 'test-1.4' 'test-Inf' 'test11.5'
>> natsort(B, '[-+]?(NaN|Inf|\d+\.?\d*)')
ans = 'test' 'test-Inf' 'test-1.4' 'test+0.3' 'test11.5' 'test+NaN'

%% Integer or decimal numbers, possibly with an exponent:
>> C = {'0.56e007', '', '43E-2', '10000', '9.8'};
>> sort(C)
ans = '' '0.56e007' '10000' '43E-2' '9.8'
>> natsort(C, '\d+\.?\d*([eE][-+]?\d+)?')
ans = '' '43E-2' '9.8' '10000' '0.56e007'

%% Hexadecimal numbers (with '0X' prefix):
>> D = {'a0X7C4z', 'a0X5z', 'a0X18z', 'a0XFz'};
>> sort(D)
ans = 'a0X18z' 'a0X5z' 'a0X7C4z' 'a0XFz'
>> natsort(D, '0X[0-9A-F]+', '%i')
ans = 'a0X5z' 'a0XFz' 'a0X18z' 'a0X7C4z'

%% Binary numbers:
>> E = {'a11111000100z', 'a101z', 'a000000000011000z', 'a1111z'};
>> sort(E)
ans = 'a000000000011000z' 'a101z' 'a11111000100z' 'a1111z'
>> natsort(E, '[01]+', '%b')
ans = 'a101z' 'a1111z' 'a000000000011000z' 'a11111000100z'

%% Case sensitivity:
>> F = {'a2', 'A20', 'A1', 'a10', 'A2', 'a1'};
>> natsort(F, [], 'ignorecase') % default
ans = 'A1' 'a1' 'a2' 'A2' 'a10' 'A20'
>> natsort(F, [], 'matchcase')
ans = 'A1' 'A2' 'A20' 'a1' 'a2' 'a10'

%% Sort order:
>> G = {'2', 'a', '', '3', 'B', '1'};
>> natsort(G, [], 'ascend') % default
ans = '' '1' '2' '3' 'a' 'B'
>> natsort(G, [], 'descend')
ans = 'B' 'a' '3' '2' '1' ''
>> natsort(G, [], 'num<char') % default
ans = '' '1' '2' '3' 'a' 'B'
>> natsort(G, [], 'char<num')
ans = '' 'a' 'B' '1' '2' '3'

%% UINT64 numbers (with full precision):
>> natsort({'a18446744073709551615z', 'a18446744073709551614z'}, [], '%lu')
ans = 'a18446744073709551614z' 'a18446744073709551615z'

Cite As

Stephen Cobeldick (2020). Customizable Natural-Order Sort (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (12)

Emmanuel Osikpa

This is extremely helpful, thanks. Feels beyond natural

Jorge Viamontes

it works thank you so much for doing this saved me a lot of time to program it myself

Thank you, it is very useful! It is as the natsort of Python.

Chang hsiung

Save me days of programming. Got this going in 1 minute.

Xiaohui Gu

Very useful!

Stephen Cobeldick

@Akim Borbuev: the function NATSORT will parse all of the input cell array, so if you only want to sort by the first column then you can use indexing to select just that column, e.g. given an nx2 cell array C:

[~,idx] = natsort(C(:,1));
D = C(idx,:);

I then used the second output from NATSORT to sort the rows of the cell array. Does that do what you want?

Akim Borbuev


I have a nx2 cell which I would like to apply your natural order sort to the first column but with respective values in the second column. Is it possible?
Thank you.


Jan van den Broecke

Junjie Wang

qiap chen

It's good tool

Chang hsiung



* Fix handling of char<num.


Total rewrite: faster and less memory.
* Remove 'asdigit' option.
* Rename 'beforechar' and 'afterchar' to 'num<char' and 'char<num'.
* Add options 'num<NaN' and 'NaN<num'.
* Improve HTML documentation.
* Include testcases.

* Consistent internal variable names.

* Improve blurb and HTML.

* Minor help edit.

* Add HTML documentation.

* Improve input checking.

* Improve binary numeric handling.
* Improve handling of skipped fields.
* Add an example of skipped field usage.

* Improved binary substring parsing.
* Better examples.

- Update documentation only, improve examples.

- Add binary numeric parsing.
- Improve input checking.
- Replace multiple debugging output arrays with one cell array.
- Allow lookarounds in regular expression.

- Simplify hexadecimal example.
- Correct output summary.

- Now parses hexadecimal and octal substrings.
- int64 and uint64 parsed at full precision.
- Allow <options> in any order.
- For debugging: return indices of character and numeric arrays.

- Implement more compact sort algorithm.
- "sscanf" numeric format can be controlled by an optional input argument.
- Provide use examples.
- Output debugging arrays now char+numeric.

- Add examples showing different numeric tokens.
- Case-insensitive sort is now default.

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