'Is Java case-sensitive string sorting broken?
I’ve tried to case-sensitive sort some strings in Java and I was quite surprised by the result. Here is the code:
List<String> words = List.of("aLocalized", "aaLocalized", "aaaLocalized", "ALocalized", "AALocalized", "AAALocalized");
System.out.println(words.stream().sorted().collect(Collectors.joining(" ")));
And here is the result: AAALocalized AALocalized ALocalized aLocalized aaLocalized aaaLocalized
To me this doesn’t look right. Why for small letters a comes before aa and aa comes before aaa, but for capital letters AAA comes before AA and AA comes before A?
Solution 1:[1]
What you maybe fail to realize is that all capital letters come before small letters. So a List.of("A", "a", "B", "b", "C", "c"); would result in A B C a b c
Solution 2:[2]
This post helped me when I faced this issue
https://cursos.alura.com.br/forum/topico-pumpandsettle-timed-out-175044
so basically remove the tester.pumpAndSettle()
and replace it with
for (int i = 0; i < 5; i++) { await tester.pump(Duration(seconds: 1)); }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ivo Beckers |
| Solution 2 | Oluwatosin Solarin |
