Natural-Order Filename Sort

Version 3.4.6 (71.7 KB) by Stephen23
Alphanumeric sort of filenames or filepaths, with customizable number format.
31.9K Downloads
Updated 1 May 2024

View License

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

To sort the elements of a string/cell array use NATSORT:
To sort the rows of a string/cell array use NATSORTROWS:
Summary
Alphanumeric sort the text in a string/cell/structure array. Sorts the text by character code taking into account the values of any number substrings. Compare for example:
>> A = {'a2.txt', 'a10.txt', 'a1.txt'};
>> sort(A)
ans = 'a1.txt' 'a10.txt' 'a2.txt'
>> natsortfiles(A)
ans = 'a1.txt' 'a2.txt' 'a10.txt'
By default NATSORTFILES interprets all consecutive digits as integer numbers, the number substring recognition can be specified using a regular expression: see NATSORT for details.
NATSORTFILES does not perform a naive natural-order sort, but sorts the filenames and file extensions separately to ensure a dictionary sort, where shorter filenames always sort before longer ones. Likewise filepaths are split at each file-separator character, and each level of the file hierarchy is sorted separately.
Example with DIR()
P = 'C:\SomeDir\SubDir';
S = dir(fullfile(P,'*.txt'));
S = natsortfiles(S);
for k = 1:numel(S)
F = fullfile(P,S(k).name)
end
File Dependency
The natural-order sort is provided by the function NATSORT (File Exchange 34464). All of NATSORT's optional inputs are supported by NATSORTFILES.
Examples
>> A = {'a2.txt', 'a10.txt', 'a1.txt'}
>> sort(A)
ans = 'a1.txt' 'a10.txt' 'a2.txt'
>> natsortfiles(A)
ans = 'a1.txt' 'a2.txt' 'a10.txt'
>> B = {'test2.m'; 'test10-old.m'; 'test.m'; 'test10.m'; 'test1.m'};
>> sort(B) % Wrong number order:
ans =
'test.m'
'test1.m'
'test10-old.m'
'test10.m'
'test2.m'
>> natsortfiles(B) % Shorter names before longer:
ans =
'test.m'
'test1.m'
'test2.m'
'test10.m'
'test10-old.m'
%% Directory Names:
>> C = {'A2-old\test.m';'A10\test.m';'A2\test.m';'A1\test.m';'A1-archive.zip'};
>> sort(C) % Wrong number order, and '-' sorts before '\':
ans =
'A1-archive.zip'
'A10\test.m'
'A1\test.m'
'A2-old\test.m'
'A2\test.m'
>> natsortfiles(C) % Shorter names before longer:
ans =
'A1\test.m'
'A1-archive.zip'
'A2\test.m'
'A2-old\test.m'
'A10\test.m'
>> D = {'A1\B', 'A+/B', 'A/B1', 'A=/B', 'A\B0'};
>> sort(D)
ans = 'A+/B' 'A/B1' 'A1\B' 'A=/B' 'A\B0'
>> natsortfiles(D)
ans = 'A\B0' 'A/B1' 'A1\B' 'A+/B' 'A=/B'
>> F = {'test_new.m'; 'test-old.m'; 'test.m'};
>> sort(F) % Note '-' sorts before '.':
ans =
'test-old.m'
'test.m'
'test_new.m'
>> natsortfiles(F) % Shorter names before longer:
ans =
'test.m'
'test-old.m'
'test_new.m'

Cite As

Stephen23 (2024). Natural-Order Filename Sort (https://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-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!
Version Published Release Notes
3.4.6

* Documentation improvements.

3.4.5

* Accept decimal comma as well as decimal point.
* HTML example use string arrays.

3.4.4

* Add testcases.

3.4.3

* Now R2009b compatible.

3.4.2

* Edit description & help.

3.4.1

* Edit description & help.

3.4.0

* Add plenty of testcases.
* Fix bug in descending sort with an empty input array.

3.3.0

* Improve test function, add test cases.
* Rename options nopath->xpath, nodot->rmdot

3.2.0

* Update TESTFUN.
* Fix bug in struct parsing.

3.1.0

* More robust TESTFUN pretty-print code.
* Add NODOT option.

3.0.5

* Improve examples.

3.0.4

* Correct summary.

3.0.3

* Improve string handling.

3.0.2

* Simplify numeric class handling.
* Add permutations test examples.

3.0.1

* handle single element with no number.

3.0.0

* Accepts and sorts the structure returned by DIR.
* Accepts and sorts a string array, categorical array, cell array of char, etc.
* Regular expression and optional arguments may be string or char.

2.1.2

* Consistent alignment tab/spaces.

2.1.1

* Add error IDs.
* Add "noext" option.

2.1.0

* Fix handling of char<num.

2.0.0

* NATSORT total rewrite: faster and less memory.
* Improve HTML documentation.
* Include testcases.

1.6.0.0

* Add (very useful) debugging output argument.

1.5.0.0

* Improve blurb and HTML.
* Improve input checking.
* Include NATSORT function.
* Add HTML documentation.
* Minor help edit.

1.4.0.0

* Clearer description of file dependency.
* Improve example of filepath sorting.

1.3.0.0

* Improve function description.
* Better examples.

1.2.0.0

- Update documentation only, improve examples.

1.1.0.0

- Complete acknowledgements.

1.0.0.0