Code covered by the BSD License  

Highlights from
Customizable Natural Order Sort

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

Customizable Natural Order Sort

by Stephen Cobeldick

 

05 Jan 2012 (Updated 24 Aug 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.

### Examples ###

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

# Integer or decimal numeric tokens, possibly with +/- signs:

B = {'File102.txt','File11.5.txt','File-1.4.txt','File+0.3.txt'};
sort(B)
 ans = {'File+0.3.txt','File-1.4.txt','File102.txt','File11.5.txt'}
sortnat(B,'(-|+)?\d+(\.\d+)?')
 ans = {'File-1.4.txt','File+0.3.txt','File11.5.txt','File102.txt'}

# Integer or decimal numeric tokens, possibly with an exponent:

C = {'A_0.56e+07','A_4.3E2','A_10000','A_9.8'};
sort(C)
 ans = {A_'0.56e+07','A_10000','A_4.3E2','A_9.8'}
sortnat(C,'\d+(\.\d+)?(e(+|-)?\d+)?')
 ans = {'A_9.8','A_4.3E2','A_10000','A_0.56e+07'}

# ASCII order (including non-printing characters):

sortnat(CellStr,'[]',true);

Acknowledgements

Sort Nat: Natural Order Sort inspired this file.

Required Products MATLAB
MATLAB release MATLAB 7.11 (R2010b)
Tags for This File  
Everyone's Tags
cell of strings, customizable, format, natural order, regexp, sort, string manipulation
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.

24 Aug 2012

- Implement more compact sort algorithm.
- "sscanf" numeric format can be controlled by an optional input argument.
- Provide use examples.
- Output debugging arrays now char+numeric.

Contact us