5.0

5.0 | 6 ratings Rate this file 122 downloads (last 30 days) File Size: 2.07 KB File ID: #10959

sort_nat: Natural Order Sort

by Douglas Schwarz

 

03 May 2006 (Updated 05 Nov 2008)

Code covered by BSD License  

Sort strings in natural order.

Download Now | 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'}

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 (7)
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!

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!

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
utilities Douglas Schwarz 22 Oct 2008 08:24:26
string manipulation Douglas Schwarz 22 Oct 2008 08:24:26
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com