简体   繁体   English

按字母顺序排列的中文 - java.text.Collat​​or

[英]Alphabetical order in Chinese - java.text.Collator

I've been testing alphabetical sorting in Chinese (if I may call it so). 我一直在用中文测试字母排序(如果我可以这么称呼的话)。 This is how Excel sorts some example words: 这是Excel对一些示例单词进行排序的方式:

啊<波<词<的<俄<佛<歌<和<及<课<了<馍<呢<票<气<日<四<特<瓦<喜<以<只 啊<波<词<的<俄<佛<歌<和<及<课<了<馍<呢<票<气<日<四<特<瓦<喜<以<只

0<2<85<!<@<版本<标记<成员<错误<导出<导航<Excel 文件<访问<分类<更改<规则<HTML<基本<记录<可选<快捷方式<类别<历史记录<密码<目录<内联<内容<讨论<文件<页面<只读 0 <2 <85 <!<@ <版本<标记<成员<错误<导出<导航<Excel文件<访问<分类<更改<规则<HTML <基本<记录<可选<快捷方式<类别<历史记录<密码<目录<内联<内容<讨论<文件<页面<只读

and this is what came out of Collections.sort(list, simplified_chinese_collator_comparator) (the first offending character in bold): 这就是Collections.sort(list, simplified_chinese_collator_comparator) (第一个以粗体显示的冒犯字符)的结果:

啊<波<词<的<俄<佛<歌<和<及<课<了<呢<票<气<日<四<特<瓦<喜<以<只< 啊<波<词<的<俄<佛<歌<和<及<课<了<呢<票<气<日<四<特<瓦<喜<以<只<

!<@<0<2<85<Excel 文件<HTML<版本<标记<成员<错误<导出<导航<访问<分类<更改<规则<基本<记录 <可选<快捷方式<类别<历史记录<密码<目录<内联<内容<讨论<文件<页面<只读 !<@ <0 <2 <85 <Excel文件<HTML <版本<标记<成员<错误<导出<导航<访问<分类<更改<规则<基本<记录<可选<快捷方式<类别<历史记录<密码<目录<内联<内容<讨论<文件<页面<只读

I don't know anything about Chinese. 我对中文一无所知。 Does anyone know why Collator output it's different, or what is it based on? 有谁知道为什么Collator输出它的不同,或者它的基础是什么?

Are there any other libraries for language-based sorting? 是否还有其他基于语言的排序库?

There isn't a Collator in Java 6 or 7 which will sort the Chinese in the same order as the first sample. Java 6或7中没有Collat​​or,它将按照与第一个样本相同的顺序对中文进行排序。

public static void main(String... args) {
    String text1 = "啊<波<词<的<俄<佛<歌<和<及<课<了<馍<呢<票<气<日<四<特<瓦<喜<以<只";
    findLocaleForSortedOrder(text1);
    String text2 = "啊<波<词<的<俄<佛<歌<和<及<课<了<呢<票<气<日<四<特<瓦<喜<以<只<馍";
    findLocaleForSortedOrder(text2);
}

private static void findLocaleForSortedOrder(String text) {
    System.out.println("For " + text + " found...");
    String[] preSorted = text.split("<");
    for (Locale locale : Collator.getAvailableLocales()) {
        String[] sorted = preSorted.clone();
        Arrays.sort(sorted, Collator.getInstance(locale));
        if (Arrays.equals(preSorted, sorted))
            System.out.println("Locale " + locale + " has the same sorted order");
    }
    System.out.println();
}

prints 版画

For 啊<波<词<的<俄<佛<歌<和<及<课<了<馍<呢<票<气<日<四<特<瓦<喜<以<只 found...

For 啊<波<词<的<俄<佛<歌<和<及<课<了<呢<票<气<日<四<特<瓦<喜<以<只<馍 found...
Locale zh_CN has the same sorted order
Locale zh has the same sorted order
Locale zh_SG has the same sorted order

Why it is different? 为什么会有所不同? Because there are several different methods of sorting ideographic characters or even entire words. 因为有几种不同的方法可以排序表意字符甚至整个单词。 The ones that stuck in my mind are: 困在我脑海中的是:

  • by number of strokes 按行程数
  • by using Latin transliteration and then ordering it "naturally" (according to rules specific for Chinese language of course) 通过使用拉丁音译然后“自然地”命名(当然根据中文的具体规则)

There are other methods as well, for example Unicode Technical Report #35 mentions some of them (more by coincidence, not necessary on purpose), but you'd have to have plenty of time to go through it. 还有其他方法,例如Unicode技术报告#35提到其中一些(更多的是巧合,不是故意的必要),但你必须有足够的时间来完成它。

To answer your question, on why these sorting orders are different, it just because Java contains its own collation rules and it does not rely on Operating System's ones (as Excel does). 为了回答你的问题,为什么这些排序顺序不同,这只是因为Java包含自己的排序规则而且它不依赖于操作系统的排序规则(就像Excel那样)。 These rules might be different. 这些规则可能有所不同。 You might also want to try out ICU , which is the source of classes and rules in Java (and is usually a step ahead than JDK). 您可能还想尝试使用ICU ,它是Java中类和规则的来源(通常比JDK领先一步)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM