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!

 채택된 답변

Parag Jhunjhunwala
Parag Jhunjhunwala 2023년 6월 23일

2 개 추천

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"

추가 답변 (1개)

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

2023년 6월 23일

답변:

2023년 6월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by