How to sort a cell alphabetically?
66 views (last 30 days)
Show older comments
Yanni
on 23 Jun 2023
Answered: Parag Jhunjhunwala
on 23 Jun 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 Comments
Accepted Answer
Parag Jhunjhunwala
on 23 Jun 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 Comments
More Answers (1)
Aakash
on 23 Jun 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 Comments
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!