微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java Collat​​ionKey排序错误

我在比较字符串时遇到问题.我想比较两个“éd”和“ef”这样的法语文本
Collator localeSpecificCollator = Collator.getInstance(Locale.FRANCE);
CollationKey a = localeSpecificCollator.getCollationKey("éd");
CollationKey b = localeSpecificCollator.getCollationKey("ef");
System.out.println(a.compareto(b));

这将打印-1,但在法语字母e中出现在é之前.但是当我们只比较e和é时

Collator localeSpecificCollator = Collator.getInstance(Locale.FRANCE);
CollationKey a = localeSpecificCollator.getCollationKey("é");
CollationKey b = localeSpecificCollator.getCollationKey("e");
System.out.println(a.compareto(b));

结果是1.你能告诉我们代码的第一部分有什么问题吗?

解决方法

这似乎是预期的行为,它似乎也是用法语按字母顺序排序的正确方法.

Android javadoc提供了一个关于它为什么表现的暗示 – 我想android中的实现细节与标准JDK类似,如果不相同的话:

A tertiary difference is ignored when there is a primary or secondary difference anywhere in the strings.

换句话说,因为您的2个字符串只能通过查看主要差异(不包括重音符号)进行排序,因此整理者不会检查其他差异.

它似乎符合Unicode Collation Algorithm (UCA)

Accent differences are typically ignored,if the base letters differ.

根据wikipedia article on “ordre alphabetique”,它似乎也是用法语按字母顺序排序的正确方法

En première analyse,les caractères accentués,de même que les majuscules,ont le même rang alphabétique que le caractère fondamental
Si plusieurs mots ont le même rang alphabétique,on tâche de les distinguer entre eux grâce aux majuscules et aux accents (pour le e,on a l’ordre e,é,è,ê,ë)

在英语中:顺序最初忽略重音和案例 – 如果2个单词无法按此方式排序,则会考虑重音和大小写.

原文地址:https://www.jb51.cc/java/128581.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐