Natural-Order Row Sort

Versione 3.4.5 (69,5 KB) da Stephen23
Alphanumeric row sort of a cell/string/categorical/table array, with customizable number format.
2K download
Aggiornato 13 lug 2023

Visualizza la licenza

Nota dell'editore: This file was selected as MATLAB Central Pick of the Week

To sort any file-names or folder-names use NATSORTFILES:
To sort the elements of a string/cell array use NATSORT:
Summary
Alphanumeric sort the rows of a string/cell/categorical/table array. Sorts the text by character code taking into account the values of any number substrings. Compare for example:
>> X = {'x2','10';'x10','0';'x1','0';'x2','2'};
>> sortrows(X) % Wrong numeric order
ans =
'x1' '0'
'x10' '0'
'x2' '10'
'x2' '2'
>> natsortrows(X) % Correct numeric order
ans =
'x1' '0'
'x2' '2'
'x2' '10'
'x10' '0'
By default NATSORTROWS interprets all consecutive digits as integer numbers, the number substring recognition can be specified using a regular expression: see NATSORT for details.
File Dependency
The natural-order sort is provided by the function NATSORT (File Exchange 34464). All of NATSORT's optional inputs are supported by NATSORTROWS.
Examples
>> A = {'B','2','X';'A','100','X';'B','10','X';'A','2','Y';'A','20','X'};
>> sortrows(A) % SORTROWS for comparison.
ans =
'A' '100' 'X'
'A' '2' 'Y'
'A' '20' 'X'
'B' '10' 'X'
'B' '2' 'X'
>> natsortrows(A)
ans =
'A' '2' 'Y'
'A' '20' 'X'
'A' '100' 'X'
'B' '2' 'X'
'B' '10' 'X'
>> natsortrows(A,[],'descend')
ans =
'B' '10' 'X'
'B' '2' 'X'
'A' '100' 'X'
'A' '20' 'X'
'A' '2' 'Y'
>> sortrows(A,[2,-3]) % SORTROWS for comparison.
ans =
'B' '10' 'X'
'A' '100' 'X'
'A' '2' 'Y'
'B' '2' 'X'
'A' '20' 'X'
>> natsortrows(A,[],[2,-3])
ans =
'A' '2' 'Y'
'B' '2' 'X'
'B' '10' 'X'
'A' '20' 'X'
'A' '100' 'X'
>> natsortrows(A,[],[false,true,true],{'ascend','descend'})
ans =
'A' '2' 'Y'
'B' '2' 'X'
'B' '10' 'X'
'A' '20' 'X'
'A' '100' 'X'
>> natsortrows(A,[],{'ignore','ascend','descend'})
ans =
'A' '2' 'Y'
'B' '2' 'X'
'B' '10' 'X'
'A' '20' 'X'
'A' '100' 'X'
T = cell2table(A);
natsortrows(T,[], [2,-3]) % TABLE
ans =
A1 A2 A3
___ _____ ___
'A' '2' 'Y'
'B' '2' 'X'
'B' '10' 'X'
'A' '20' 'X'
'A' '100' 'X'
natsortrows(T,[], {'A2','A3'},{'ascend','descend'}) % TABLE
ans =
A1 A2 A3
___ _____ ___
'A' '2' 'Y'
'B' '2' 'X'
'B' '10' 'X'
'A' '20' 'X'
'A' '100' 'X'
>> B = {'ABCD';'3e45';'67.8';'+Inf';'-12';'+9';'NaN'};
>> sortrows(B) % SORTROWS for comparison.
ans =
'+9'
'+Inf'
'-12'
'3e45'
'67.8'
'ABCD'
'NaN'
>> natsortrows(B,'[-+]?(NaN|Inf|\d+\.?\d*(E[-+]?\d+)?)')
ans =
'-12'
'+9'
'67.8'
'3e45'
'+Inf'
'NaN'
'ABCD'
>> C = {'A2', 2; 'A10', 1; 'A2', 1}; % Scalar numerics in a column:
>> natsortrows(C,[],'sortnum')
ans =
'A2' [1]
'A2' [2]
'A10' [1]
>> natsortrows(C,[],'sortnum',{'ascend','descend'})
ans =
'A2' [2]
'A2' [1]
'A10' [1]

Cita come

Stephen23 (2024). Natural-Order Row Sort (https://www.mathworks.com/matlabcentral/fileexchange/47433-natural-order-row-sort), MATLAB Central File Exchange. Recuperato .

Compatibilità della release di MATLAB
Creato con R2010b
Compatibile con R2009b e release successive
Compatibilità della piattaforma
Windows macOS Linux
Categorie
Scopri di più su Characters and Strings in Help Center e MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Versione Pubblicato Note della release
3.4.5

* Accept decimal comma as well as decimal point.
* HTML example use string arrays.

3.4.4

* Add testcases.

3.4.3

* Now R2009b compatible.

3.4.2

* Edit description & help.

3.4.1

* Edit description & help.

3.4.0

* Add plenty of testcases.
* Fix bug in descending sort with an empty input array.
* Add feature: sort tables, selection by table variable/column.
* Add feature: sort scalar numerics in cell array.

3.3.0

* Improve test function, add test cases.

3.2.0

* Update TESTFUN.

3.1.0

* More robust TESTFUN pretty-print code.

3.0.5

* Improve examples.

3.0.4

* Correct summary.

3.0.3

* Improve string handling.

3.0.2

* Simplify numeric class handling.
* Add permutations test examples.

3.0.1

* handle single element with no number.

3.0.0

* Accepts and sorts a string array, categorical array, cell array of char, etc.
* Regular expression and optional arguments may be string or char.

2.1.2

* Consistent alignment tab/spaces.

2.1.1

* Add error IDs.

2.1.0

* Fix handling of char<num.

2.0.0

* NATSORT total rewrite: faster and less memory.
* Remove second input COL, is now supplied as optional argument.
* Improve HTML documentation.
* Include testcases.

1.6.0.0

* Add (very useful) debugging output argument.

1.5.0.0

* Improve blurb and HTML.
* Minor help edit
* Improve input checking.
* include NATSORT function.
* Add HTML documentation.

1.4.0.0

* Fix missing image.
* Include screenshot.
* Clearer description of file dependency.

1.3.0.0

* Improve function description.
* Better examples.

1.2.0.0

- Update documentation only, improve examples.

1.1.0.0

- Complete acknowledgements.

1.0.0.0