Code covered by the BSD License  

Highlights from
Customizable Natural Order Sort

Be the first to rate this file! 7 Downloads (last 30 days) File Size: 2.73 KB File ID: #34464

Customizable Natural Order Sort

by Stephen Cobeldick

 

05 Jan 2012 (Updated 14 Feb 2012)

Faster "Natural Order" sort of a Cell-of-Strings, with customizable numeric format.

| Watch this File

File Information
Description

### Function ###

A "natural order" sort is where any numeric values occuring within the strings is taken into account in the sort. Compare for example:

>> A = {'File2.txt','File10.txt','File1.txt'};

>> sort(A)
   ans = {'File1.txt','File10.txt','File2.txt'}

>> sortnat(A)
   ans = {'File1.txt','File2.txt','File10.txt'}

# This function provides optional user-control over the numeric value format, allowing plus/minus signs, decimal points, exponents, etc., to be taken into account in the numeric values.

# This function is faster than other "natural order" Mfiles provided on MATLAB File Exchange.

### Example Usage ###

# The default is for integer values only, as shown above.

# Strings with decimal values, some with +/- signs:

>> B = {'File1.3.txt','File1.4.txt','File+0.3.txt','File-1.4.txt'};

>> sort(B)
  ans = {'File+0.3.txt','File-1.4.txt','File1.3.txt','File1.4.txt'}

>> sortnat(B,'(-|+)?\d+\.\d+')
   ans = {'File-1.4.txt','File+0.3.txt','File1.3.txt','File1.4.txt'}

# Strings with integer values, some as exponents:

>> C = {'File10e5.txt','File1000000.txt','File100.txt','File10e3.txt'};

>> sort(C)
   ans = ={'File100.txt','File1000000.txt','File10e3.txt','File10e5.txt'}

>> sortnat(C,'(10e)?\d+')
   ans = {'File100.txt','File10e3.txt','File10e5.txt','File1000000.txt'}

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
sort_nat: Natural Order Sort

Required Products MATLAB
MATLAB release MATLAB 7.11 (2010b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Updates
14 Feb 2012

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

Tag Activity for this File
Tag Applied By Date/Time
natural order Stephen Cobeldick 05 Jan 2012 16:07:51
sort Stephen Cobeldick 05 Jan 2012 16:07:51
cell of strings Stephen Cobeldick 05 Jan 2012 16:07:51
string manipulation Stephen Cobeldick 05 Jan 2012 16:07:51
customizable Stephen Cobeldick 05 Jan 2012 16:07:51
regexp Stephen Cobeldick 05 Jan 2012 16:07:51
format Stephen Cobeldick 05 Jan 2012 16:07:51

Contact us at files@mathworks.com