# Very simple question about strings

1 visualizzazione (ultimi 30 giorni)
Sameer il 1 Ago 2014
Modificato: Stephen23 il 27 Mag 2015
Hello all
I have a cell containing stings something like this:
A={'SI1','SI12','SI13','SI2'};
I have to arrange these strings either in ascending order or in descending based on the numbers they are having. Can anyone please guide?
Regards
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Stephen23 il 27 Mag 2015
Modificato: Stephen23 il 27 Mag 2015
You can simply use my FEX submission natsort, which performs a natural order sort on a cell array of strings:
>> A = {'SI1','SI12','SI13','SI2'};
>> natsort(A)
ans =
'SI1' 'SI2' 'SI12' 'SI13'

Accedi per commentare.

### Risposta accettata

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);
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);
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (1)

Honglei Chen il 1 Ago 2014
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by