image thumbnail

Natural-Order Row Sort

バージョン 3.4.4 (23.8 KB) 作成者: Stephen23
Alphanumeric row sort of a cell/string/categorical/table array, with customizable number format.

ダウンロード 1.6K 件

更新 2022/1/31

ライセンスの表示

編集メモ: 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]

引用

Stephen23 (2022). Natural-Order Row Sort (https://www.mathworks.com/matlabcentral/fileexchange/47433-natural-order-row-sort), MATLAB Central File Exchange. 取得済み .

MATLAB リリースの互換性
作成: R2010b
R2009b 以降と互換性あり
プラットフォームの互換性
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!