Filter löschen
Filter löschen

How to sort a cell alphabetically?

64 Ansichten (letzte 30 Tage)
Yanni
Yanni am 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)
ans = 4×1 string array
"AM" "AOP" "AbnormalAccruals" "AccrualsBM"
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!

Akzeptierte Antwort

Parag Jhunjhunwala
Parag Jhunjhunwala am 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)
A = 4×1 string array
"AbnormalAccruals" "AccrualsBM" "AM" "AOP"

Weitere Antworten (1)

Aakash
Aakash am 23 Jun. 2023

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by