Skip to content

Commit 7cb5462

Browse files
committed
rename alphabetical to lexicographical sorting and add alphanumerical sorting option (both in case-sensitive and case-insensitive versions)
1 parent 3c325cc commit 7cb5462

1 file changed

Lines changed: 74 additions & 4 deletions

File tree

src/tools_lgpl/matlab/quickplot/progsrc/private/sort_stations.m

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
%sort_stations Routine to sort station names
33

44
NONE = 'No Sorting';
5-
ALPHABETICAL = 'Sorted Alphabetically';
5+
LEXICOGRAPHICAL = 'Lexicographical Sorting';
6+
LEXICOGRAPHICAL_CI = 'Case-insensitive Lexicographical Sorting';
7+
ALPHANUMERICAL = 'Alpha-Numerical Sorting';
8+
ALPHANUMERICAL_CI = 'Case-insensitive Alpha-Numerical Sorting';
69

710
if nargin == 1 && nargout <= 1
811
switch in1
912
case 'methods'
10-
out1 = {NONE,ALPHABETICAL};
13+
out1 = {NONE,LEXICOGRAPHICAL,LEXICOGRAPHICAL_CI,ALPHANUMERICAL,ALPHANUMERICAL_CI};
1114
case 'default'
1215
out1 = NONE;
1316
otherwise
@@ -20,10 +23,77 @@
2023
out1 = in1;
2124
out2 = 1:length(in1);
2225

23-
case ALPHABETICAL
26+
case LEXICOGRAPHICAL
2427
[out1,out2] = sort(in1);
2528

29+
case LEXICOGRAPHICAL_CI
30+
[~,out2] = sort(lower(in1));
31+
out1 = str(out2);
32+
33+
case ALPHANUMERICAL
34+
out2 = alphanumSort(in1);
35+
out1 = in1(out2);
36+
37+
case ALPHANUMERICAL_CI
38+
out2 = alphanumSort(lower(in1));
39+
out1 = in1(out2);
40+
2641
otherwise
2742
error('Sort method %s not implemented.', stationSortMethod)
2843
end
29-
end
44+
end
45+
46+
function idx = alphanumSort(str)
47+
%ALPHANUMSORT Natural (alphanumeric) sort of strings
48+
%
49+
% idx = alphanumSort(str)
50+
%
51+
% Input:
52+
% str - cell string OR string array
53+
%
54+
% Output:
55+
% idx - sorting order
56+
57+
% Accept cell string and string array
58+
if ~isstring(str) && ~iscellstr(str)
59+
error('Input must be a cell a cell string or a string array.');
60+
end
61+
62+
tokens = cellfun(@split, str, 'UniformOutput', false);
63+
nTokens = cellfun(@numel,tokens);
64+
maxTokens = max(nTokens);
65+
key = repmat({'',0},numel(str), ceil(maxTokens/2));
66+
67+
for i = 1:numel(str)
68+
key(i,1:nTokens(i)) = tokens{i};
69+
end
70+
71+
% Sort rows lexicographically
72+
[~, idx] = sortrows(key);
73+
74+
75+
function tokens = split(str)
76+
isNumber = str>='0' & str<='9';
77+
tokenStart = find(diff(isNumber)) + 1;
78+
nTokens = numel(tokenStart)+1;
79+
startsWithNumber = isNumber(1);
80+
81+
blankedStr = str;
82+
blankedStr(~isNumber) = ' ';
83+
values = num2cell(sscanf(blankedStr,'%u'));
84+
85+
tokens = cell(1,nTokens+startsWithNumber);
86+
for i = 1+startsWithNumber:2:nTokens
87+
if i == 1
88+
if nTokens == 1
89+
tokens{i} = str;
90+
else
91+
tokens{i} = str(1:tokenStart(i)-1);
92+
end
93+
elseif i == nTokens
94+
tokens{i} = str(tokenStart(i-1):end);
95+
else
96+
tokens{i} = str(tokenStart(i-1):tokenStart(i)-1);
97+
end
98+
end
99+
tokens(2-startsWithNumber:2:end) = values;

0 commit comments

Comments
 (0)