我想通过中文笔画数来排序列表,并且我正在用这种方法创建排序器来实现它,但是似乎带有TRADITIONAL_CHINESE的排序器有一些错误。
举个例子,
List<String> strList = new List<>();
strList.add("日");
strList.add("蘋");
Collections.sort(strList, new SortChinese());
class SortChinese implements Comparator<String> {
public int compare(String obj1, String obj2) {
Collator collator = Collator.getInstance(Locale.TRADITIONAL_CHINESE);
collator.setStrength(Collator.PRIMARY);
return collator.compare(obj1, obj2);
}
}以前:日,蘋
后:蘋,日
预期结果:日,蘋
对于在android中中文笔画计数排序有什么想法或建议吗?
发布于 2018-01-10 09:13:21
您可以删除ShortChinese:
List<String> strList = new List<>();
strList.add("日");
strList.add("蘋");
Collator collator = Collator.getInstance(Locale.TRADITIONAL_CHINESE);
collator.setStrength(Collator.PRIMARY);
Collections.sort(strList, collator);发布于 2018-02-22 06:09:47
你可以试试这个:
strList.sort((o1, o2) -> compareInChinese(str1, str2));也许您可以将此添加到您的util类中。
public static int compareInChinese(String str1, String str2) {
Collator collator = Collator.getInstance(new Locale("zh", "CN"));
collator.setDecomposition(Collator.NO_DECOMPOSITION);
collator.setStrength(Collator.PRIMARY);
return collator.compare(str1, str2);
}发布于 2018-01-10 09:40:23
@propoLis的代码适用于我(Java 8,Windows)。用UTF-8编辑和汇编。也许您使用的是其他编码,而不是编译的。
所以,试一试:
strList.add(new String(new int[] {0x65e5}, 0, 1)); // Unicode code points
strList.add(new String(new int[] {0x860b}, 0, 1));或
strList.add("\u65e5"); // Unicode UTF-16
strList.add("\u860b");然而,您需要两个错误才能再次收到中文。
https://stackoverflow.com/questions/48182478
复制相似问题