Ryt*_*kna 5 java unicode collation
我有一个要按第一个字母排序的 unicode 字符串列表。问题是我不知道设置 java.text.Collator 它将相似的字母视为不同的。
这就是我现在得到的:
这就是我想要得到的(以Š开头的单词应该总是跟在S后面而不是第二个字母):
我们可以创建一个扩展 Collator 的类并重写其中的比较方法。
\n\n这里有一个例子。
\n\n公共类 MyCollator 扩展 Collator {
\n\n@Override\npublic int compare(String source, String target) {\n return source.compareTo(target);\n}\n\n@Override\npublic CollationKey getCollationKey(String source) {\n // TODO Auto-generated method stub\n return null;\n}\n\n@Override\npublic int hashCode() {\n // TODO Auto-generated method stub\n return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n}
\n\n然后我们可以使用这个新添加的类对String列表进行排序,并且它将以正确的方式显示。
\n\n整理器整理器 = new MyCollator();
\n\nCollections.sort(列表,整理器);
\n\n我的测试结果如下:
\n\n请注意,在结果中,\xc5\xa0显示在T之后,这是因为"\xc5\xa0".compareTo("T")>1等于 true。
\n\n我相信您可以在比较方法中添加一些逻辑,使 \xc5\xa0显示在S之后,但在T之前。
\n\n上述代码是使用JDK 1.5版本编译并执行的。
\n\n直接使用Collections.sort(list) ;您将得到与我上面提到的相同的结果。
\n