简体   繁体   English

Java Unicode字符串排序

[英]Java Unicode strings sorting

In Java, how does Unicode strings get compared? 在Java中,如何比较Unicode字符串?

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? 这些字符串如何进行比较和排序?

By default, Strings sort lexicographically, by Unicode order. 默认情况下,字符串按字典顺序按Unicode顺序排序。 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. 订单是UTF-16,因此可能不是您想要的某些字符,但日语字符都在BMP中 ,所以您不应该遇到这些问题。

If you would like a different sort order, you can use the java.text.Collator classes to define a different sort order. 如果您想要不同的排序顺序,可以使用java.text.Collator类来定义不同的排序顺序。

By default it's in UTF-16 byte-code comparison. 默认情况下,它采用UTF-16字节码比较。 This is the fastest way, and hence perfect if all you need is some order (eg 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. 如果您需要对给定语言环境中的用户有意义的排序,请使用java.text.Collat​​or类。

According to compareTo methodof String class. 根据compareTo方法的String类。 See the javadoc : 看到javadoc

Compares two strings lexicographically. 按字典顺序比较两个字符串。 The comparison is based on the Unicode value of each character in the strings. 比较基于字符串中每个字符的Unicode值。 The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. String对象表示的字符序列按字典顺序与参数字符串表示的字符序列进行比较。 The result is a negative integer if this String object lexicographically precedes the argument string. 如果此String对象按字典顺序位于参数字符串之前,则结果为负整数。 The result is a positive integer if this String object lexicographically follows the argument string. 如果此String对象按字典顺序跟随参数字符串,则结果为正整数。 The result is zero if the strings are equal; 如果字符串相等,结果为零; compareTo returns 0 exactly when the {@link #equals(Object)} method would return true . compareTo恰好在{@link #equals(Object)}方法返回true时返回0

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

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