Code covered by the BSD License  

Highlights from
sort_nat: Natural Order Sort

5.0

5.0 | 17 ratings Rate this file 51 Downloads (last 30 days) File Size: 2.26 KB File ID: #10959

sort_nat: Natural Order Sort

by Douglas Schwarz

 

03 May 2006 (Updated 22 Jan 2011)

Sort strings in natural order.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

Natural order sorting sorts strings containing digits in a way such that the numerical value of the digits is taken into account. It is especially useful for sorting file names containing index numbers with different numbers of digits. Often, people will use leading zeros to get the right sort order, but with this function you don't have to do that. For example, with input of

{'file1.txt','file2.txt','file10.txt'}

a normal sort will give you

{'file1.txt','file10.txt','file2.txt'}

whereas, sort_nat will give you

{'file1.txt','file2.txt','file10.txt'}

Acknowledgements
This submission has inspired the following:
Customizable Natural Order Sort
MATLAB release MATLAB 7.0.4 (R14SP2)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (23)
26 Jun 2006 Mike Palumbo

Great, much better than another file on the exchange "sortn", sortn froze when trying to sort over 6000 strings, this sort did it just fine in about a second.

13 Sep 2006 F Moisy

Perfect. Should be included in R2007a!

14 Dec 2006 Hans van Dijk

Great, just what I needed.

02 Aug 2007 Gang Xu

Nice! Thanks

25 May 2008 Nikola Toljic

Thanks

29 Aug 2008 Greg Fichter

Nifty, thanks.

09 Dec 2008 Adam Baker

I wish I'd found this sooner!

01 Apr 2010 Pete

Good work.

Is it just me though or would this not benefit from being able to specify the direction? This would be as simple as:

function [cs,index] = sort_nat(c,varargin)

... <entire code>

if nargin>1
    if strcmpi(varargin{1},'descend')
        index = flipud(index);
        cs = c(index);
    end
end

06 Apr 2010 Douglas Schwarz

Pete, I'm not sure sorting in descending order will be used very often, but, as you say, it's easy to add so why not? I chose to do it in a slightly different way, but it works the way you want. Thanks for the suggestion. Update should appear soon.

22 Aug 2010 Pete

Excellent. 5 stars.

I think I ran into the need for a descending sort because I was looking to search through various files with timestamps in their name for the most recent entry (of something-or-other).

Anyway, good work.

12 Sep 2010 Oscar  
22 Jan 2011 Evgeny Pr

Hi! Very good function and coding!
I could not write such a elegant and fast function. :)

One question:
You would not want to make sort mode a case-insensitive?

For Example:
c = {'a1', 'a2', 'a10', 'b1', 'X1', 'A10'}

Case-sensetive sort:
cs = {'A10', 'X1', 'a1', 'a2', 'a10', 'b1'}

Case-insensitive sort:
cs = {'a1', 'a2', 'a10', 'A10', 'b1', 'X1'}

22 Jan 2011 Evgeny Pr

This code not entirely correct:
if strcmpi(mode,'ascend')
    cs = c(index);
else
    cs = c(index(end:-1:1));
end

Output argument INDEX does not change depending on the sort order.

Better to do so:
if strcmpi(mode, 'descend')
    index = flipud(index);
end
cs = c(index);
index = reshape(index, size(cs)); % same as C array dimension

22 Jan 2011 Douglas Schwarz

Evgeny, you must be the first person to use descending order and the index. You are quite right, it's a bug and I thank you for identifying it. I fixed it in a slightly different way and just uploaded the new version.

To do case-insensitive sorting you can just do this:
c = {'a1', 'a2', 'a10', 'b1', 'X1', 'A10'};
[unused,index] = sort_nat(lower(c));
cs = c(index);

I want sort_nat to work the same way as sort and since sort doesn't do case-insensitive sorting I left that out of sort_nat as well.

Regards,
Doug

22 Jan 2011 Evgeny Pr

Douglas Schwarz,
You offered the perfect solution for case-insensitive sorting.
Thank you!

10 Feb 2011 Edwin Carter

Great, works straight off in Octave too.

27 Apr 2011 Carsten Bolwien  
26 May 2011 Jun

Many thanks, save me time and effort to write my own code to crack this problem

05 Jul 2011 Johanna  
28 Jul 2011 Ben

This is great! Better to include it in the Matlab!

03 Jan 2012 Damon Bradley  
03 Jan 2012 Damon Bradley

Excellent work. Saved me a much time and headache with some data analysis over here at NASA. Thank you!

01 May 2012 Shamir Alavi

I have wasted a lot of time trying to properly sort my image files for my image processing project before I surfed on to this code. And mathworks web tutorial on this is totally flawed. Great work! Certainly deserves a 5 star.

Please login to add a comment or rating.
Updates
18 Sep 2006

Fixed ambiguity of sort order in certain cases e.g., {'a0','a00'}. Increased speed. Relaxed MATLAB version requirements -- no longer requires R2006a, should work with much older versions now.

05 Nov 2008

Steve Herman identified an obscure bug (sorting a cell array of one string which has no numeric characters) which has now been fixed. Thank you Steve!

06 Apr 2010

Added ability to sort in descending order.

22 Jan 2011

Fixed bug identified by Evgeny Pr. (Thanks!)

Tag Activity for this File
Tag Applied By Date/Time
strings Douglas Schwarz 22 Oct 2008 08:24:26
sort Douglas Schwarz 22 Oct 2008 08:24:26
natural order Douglas Schwarz 22 Oct 2008 08:24:26
digits Douglas Schwarz 22 Oct 2008 08:24:26
string manipulation Douglas Schwarz 22 Oct 2008 08:24:26
utilities Douglas Schwarz 22 Oct 2008 08:24:26
potw Lindsay Coutinho 26 Apr 2011 14:44:45
pick of the week Lindsay Coutinho 26 Apr 2011 14:44:45

Contact us at files@mathworks.com