Java Unicode 字符串排序
在Java中,如何比较Unicode字符串?
我的意思是,如果我有一些日语字符串,当我执行以下操作时:
java.util.Arrays.sort(arrayOfJapaneseStrings);
如何比较和排序这些字符串?
In Java, how does Unicode strings get compared?
What I mean is, if I have a few say, Japanese strings, when I do the following:
java.util.Arrays.sort(arrayOfJapaneseStrings);
how does those strings get compared and sorted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,字符串按 Unicode 顺序按字典顺序排序。该顺序采用 UTF-16,因此对于某些字符可能不完全符合您的要求,但日语字符均位于 BMP 中,所以你应该不会对这些有问题。
如果您想要不同的排序顺序,可以使用 java.text.Collator 类来定义不同的排序顺序。
By default, Strings sort lexicographically, by Unicode order. The order is by UTF-16, so might not be exactly what you want for certain characters, but Japanese characters are all in the BMP, so you shouldn't have a problem with these.
If you would like a different sort order, you can use the
java.text.Collator
classes to define a different sort order.默认情况下,它采用 UTF-16 字节码比较。这是最快的方法,因此如果您需要的只是某种顺序(例如,如果您稍后要使用二分搜索,则需要它们按顺序排列,但只是“按顺序排列”),那么这是完美的方法" 的意思并不重要,所以越快越好)。
如果您需要对给定语言环境中的用户有意义的排序,请使用 java.text.Collator 类。
By default it's in UTF-16 byte-code comparison. This is the fastest way, and hence perfect if all you need is some order (e.g. if you are going to use a binary search later, you need them to be in order, but just what "in order" means doesn't matter, so the faster the better).
If you need an ordering that is sensible to a user in a given locale, use the java.text.Collator class.
根据String类的
compareTo
方法。请参阅 javadoc :According to
compareTo
methodof String class. See the javadoc: