我有一组连字符的字符串集。考虑到语言环境,我想对其进行排序。
List<String> words = Arrays.asList("App - Small", "Apple", "App - Big");
Collator collator = Collator.getInstance(new Locale("en"));
// Sort Method 1
Collections.sort(words, String.CASE_INSENSITIVE_ORDER);
System.out.println(words.toString());
// Sort Method 2
collator.setStrength(Collator.PRIMARY);
Collections.sort(words, collator);
System.out.println(words.toString());
结果
String.CASE_INSENSITIVE_ORDER
[App - Big, App - Small, Apple]
Collator.PRIMARY
[App - Big, Apple, App - Small]
虽然 Collator.PRIMARY 应该进行不区分大小写的排序,但使用上述两种方法的顺序之间存在差异。如何实现与连字符一起使用的基于区域设置的不区分大小写的排序顺序。
[App - Big, App - Small, Apple] - 预期的排序顺序