Very simple question about strings
1 visualizzazione (ultimi 30 giorni)
I have a cell containing stings something like this:
I have to arrange these strings either in ascending order or in descending based on the numbers they are having. Can anyone please guide?
David Young il 1 Ago 2014
"According to the numbers" is ambiguous.
This sorts the strings in ascending order, making the digit in position 3 more significant than the digit in position 4, etc.:
Asorted = sort(A);
Your example is already sorted by this criterion.
This, on the other hand, makes the last digit the least significant, regardless of how many digits there are. It assumes that there are always 2 characters to ignore at the start:
nums = cellfun(@(s) str2double(s(3:end)), A);
[~, indexes] = sort(nums);
Asorted = A(indexes);