Bill Denney wrote:
I'm wanting to sort cell arrays of chars in a smart way.
Essentially, I'd like to do something like:
A3B10
A3B9
A10
A1
A2
becomes
A1
A2
A3B9
A3B10
A10
so that it will sort the numeric portions intelligently. Does anyone
have a routine that will do that or is there a built-in sort that I
don't know of?
Thanks,
Bill
This is not a typical way to sort the above... For example sorting
cell arrays gives
octave:1> a{1} = "A3B10"; a{2} = "A3B9"; a{3} = "A10"; a{4} = "A1";
a{5} = "A2";
octave:2> a
a =
{
[1,1] = A3B10
[1,2] = A3B9
[1,3] = A10
[1,4] = A1
[1,5] = A2
}
octave:3> sort(a)
ans =
{
[1,1] = A1
[1,2] = A10
[1,3] = A2
[1,4] = A3B10
[1,5] = A3B9
}
which respects the alphabetical order. Sorting of the first numerical
field as you've done would mean that you would have to extract this
numerical field in some manner. If these are indexes in to a table
with index A and B, why not store this as
A(1) = 3; A(2) = 3; A(3) = 10; A(4) = 1; A(5) = 2;
B(1) = 10; B(2) = 9; B(3) = 0; B(4) = 0; B(5) = 0;
[val, idx] = sort (A);
[dummy, idx2] = unique (val);
i = find(diff(idx2) > 1);
idx1 = idx2(i)+1;
idx2 = idx2(i+1);
for i = 1:length(idx1)
[dummy, idx(idx1(i):idx2(i))] = sort (A(idx(idx1(i):idx2(i))));