How to sort a cell alphabetically?
56 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Yanni
le 23 Juin 2023
Réponse apportée : Parag Jhunjhunwala
le 23 Juin 2023
I want to sort a cell alphabetically, but when I using sort(), I got result as below:
A = ["AM";"AbnormalAccruals";"AOP";"AccrualsBM"];
sort(A)
However, the result I want is as follows:
"AbnormalAccruals"
"AccrualsBM"
"AM"
"AOP"
That is, the letter 'b' and 'c' should be arranged before the letter 'M'.
Thank you very much!
0 commentaires
Réponse acceptée
Parag Jhunjhunwala
le 23 Juin 2023
The following code sorts a cell alphabetically by converting all the strings to lowercase characters without affecting the original cell:
A = ["AM";"AbnormalAccruals";"AOP";"AccrualsBM"];
[~,idx]=sort(lower(A));
A=A(idx)
0 commentaires
Plus de réponses (1)
Aakash
le 23 Juin 2023
You can use the function provided in this matlab file exchange resource: https://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort?s_tid=srchtitle_sort%20strings_2
0 commentaires
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!