File Exchange

image thumbnail

Natural-Order Filename Sort

version 3.1.0 (22.3 KB) by Stephen Cobeldick
Alphanumeric sort of filenames or filepaths, with customizable numeric format.

358 Downloads

Updated 10 Jun 2021

View Version History

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 strings 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 ###
>> 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

Stephen Cobeldick (2021). Natural-Order Filename Sort (https://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (71)

Rostyslav Olshevskyi

Lachlan Darch

So good

Very useful, thanks!

Elysi Cochin

Lingjun Ding

pranav wani

Stephen Cobeldick

"Where is the source?"
In the .zip file.
"in the zip file there is no source (.m file) but examples."
1) I just tried downloading it now, and the zip file contained all of the Mfiles that I would expect.
2) I can see the Mfiles listed and displayed in the Functions tab (above), which also tells us that they are in the zip file.

ganbayar batchuluun

Where is the source? in the zip file there is no source (.m file) but examples.

Arjun Balakrishnan

Great Job, thanks!

Joao Nunes

Super useful!

Sophia Boucher

Sateesh Kandukuri

Thank you so much for the add on function.

Thi Minh Anh Quang

Chang hsiung

Daniel Robertson

Thank you so much for this. You just saved me a lot of time.

Alexander Bartels

arnold

alright... I have to take back my previous comment... I had older versions of it, there IS an ascend/descend option now!
Great contribution!

Yaad

Ameer Hamza

PUNEET KOLI

I was struggling for a month. This was exactly what I needed. Thank you so much

Brett Grant

So helpful - many thanks to the author

John Kearns

GeunHwan Kim

Creatlee

Jenna

easy to use and does the job

Sergiy Lukin

Works like a charm, thanks a lot for sharing this!

H.S

Thank you!

Jure Tica

Just what I needed. Thanks!

Lars Loetgering

Michael Samaan

Nice work!

Ahmad Obeid

Excellent work

Antonio Susin

Aditya Kiran GH

Loved it. Saved me so much effort.

Zeba Ahmad

Peihan Tu

Stefano Caschera

Dhyana Ramamurthy

Very helpful. Thanks for the contribution.

Arslan Ahmed Awan

Thanks You.

Anu_M

Brilliant!

Kiran Ps

jing yang

very useful function, thanks for your sharing!

Sören Hufschmidt

Niravkumar Ambaliya

Works perfectly. Awesome work. Thanks.

Luis Ciria

Nice!

TekTao

TekTao

Jurgen vL

Yang Ran

Thanks so much!

Kevin Ho

Wonderfully helpful function. Thank you.

Tatyana P

Thanks! Great thing helps a lot!

Canberk Suat Gurel

Maria M.

Thanks so much, very helpful!

Rahul Kumar Sevakula

Thank you. This is excellent. Works as expected.

Paolo

Jan

This is a powerful, reliable, efficient and well documented tool. The natural sorting of strings or filenames is needed in many situations. Thank you.

Alutsyah Luthfian

Very helpful! Thanks...
And also your constant reminder of not using eval, I am very thankful for it also.

Ayu Dyah

thanks a lot!

akb akbar

Thanks a lot,
really helped me.

hugstone

thanks very much!

love me love my dog

thanks!

Sanggon Kim

Thank you very much.

sid.sapien

Thanks, was very handy.

Stephen Cobeldick

@Will: it is not so easy "to list files completely like Windows". Windows places non-alphanumeric printing characters first, but their order is not clearly specified: do you know why Windows sorts '~' before '+', and '_' before '=', even though this is not their character code order? I don't. What other special character orders does Windows have? Can you show me where this order is specified?

Then there is the question of _which_ Windows sort order: the file explorer sort order or the powershell sort order? The Win2K sort order, the MS Excel sort order, or the Vista/Win7 sort order? They are all different... and then why just Windows OS, what about other major OS's? Which ones?

Not only is the "Windows sort order" very vaguely defined, this change would actually break NATSORT: I clearly state that NATSORT sorts according to two simple criteria: character code and numeric value. This means NATSORT can provide the same sort as many other "Natural Order Sort" functions written in other languages (do an internet search), which all sort according to the same basic rules as my submission and can provide the same sort order using the very precisely defined character code order. What you are suggesting is to replace the openly-defined and universally-known character code order with a badly-defined proprietary sort order. Interesting...

But it is certainly _possible_ to write a function that does this: show me a reference that defines the "Windows sort order" and then I can help you.

Will

This was really useful, however I've just found that where Windows will list files with "_" higher than those with numbers, natsort and natsortfiles will list "_" after numbers and letters.

E.g. Windows lists:
_File
1. File 1
2. File 2
a File
B File

And in MATLAB:

>> natsort({'1. File 1';'2. File 2';'a File';'B File';'_File'})

ans =

'1. File 1'
'2. File '
'a File'
'B File'
'_File'

Could you please implement this in an update? Or advise how to customise the functions to list files completely like Windows?

Ulysses C

Changjie Guan

It's really useful! Thx.

Moses

Wow, my hats off to you sir. Great function and documentation. Many Thanks!

Moses

sada shiva

Stelios

Thanks, that's exactly what I was looking for to sort namefiles formatted as
1-d...,2-d...,...18-d...etc.

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

Community Treasure Hunt

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

Start Hunting!