Java 中的字典顺序

发布于 2024-12-12 03:49:01 字数 266 浏览 0 评论 0原文

Java 中的字典顺序是如何定义的,尤其是涉及 !. 等特殊字符时?

示例顺序可以在此处找到,

但是 Java 是如何定义它的顺序的呢?我问这个问题是因为我在 Java 和 Oracle 上对字符串进行排序,得到了不同的结果,并且找不到字典顺序的规范。

How is the lexicographic order defined in Java especially in reference to special characters like !, . and so on?

An examplary order can be found here

But how does Java define it's order? I ask because I'm sorting Strings on Java and on Oracle and come up with different results and can't find the specification for the lexicographic order.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落日海湾 2024-12-19 03:49:01

来自 String.compareTo

按字典顺序比较两个字符串。比较基于字符串中每个字符的 Unicode 值。

这是字典顺序的定义。如果两个字符串不同,则它们在某个索引处具有不同的字符(该索引是两个字符串的有效索引),或者它们的长度不同,或者两者都不同。如果它们在一个或多个索引位置具有不同的字符,则令 k 为此类索引中的最小者;那么位置 k 处的字符具有较小值的字符串,通过使用 < 确定运算符,按字典顺序位于另一个字符串之前。在本例中,compareTo 返回两个字符串中位置 k 处的两个字符值的差值 [...]

因此基本上,它将每个字符串视为 16 位无符号整数序列。没有文化意识,不理解复合字符等。如果您想要更复杂的排序,您应该查看 Collat​​or

From the docs for String.compareTo:

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.

and

This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string [...]

So basically, it treats each string like a sequence of 16-bit unsigned integers. No cultural awareness, no understanding of composite characters etc. If you want a more complex kind of sort, you should be looking at Collator.

北方。的韩爷 2024-12-19 03:49:01

在 Java 中,它基于字符串的 Unicode 值:

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#compareTo(java.lang.String)

在Oracle中,这取决于在您正在使用的字符集上您的数据库将希望它是 UTF-8,以便与 Java 具有一致的行为。

要检查字符集:

SQL> SELECT parameter, value FROM nls_database_parameters 
     WHERE parameter = 'NLS_CHARACTERSET';

PARAMETER             VALUE 
------------------    ---------------------
NLS_CHARACTERSET      UTF8

如果它不是 UTF-8,则根据您的 Oracle 数据库使用的字符集,您可以获得不同的比较行为。 。

In Java it's based on the Unicode value of the string:

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#compareTo(java.lang.String)

In Oracle, it will depend on the charset you are using on your database. You'll want it to be UTF-8 to have consistent behavior with Java.

To check the character set:

SQL> SELECT parameter, value FROM nls_database_parameters 
     WHERE parameter = 'NLS_CHARACTERSET';

PARAMETER             VALUE 
------------------    ---------------------
NLS_CHARACTERSET      UTF8

If it's not UTF-8, then you can get different comparison behavior depending on which character set your Oracle database is using.

故人的歌 2024-12-19 03:49:01

来自 javadocs

比较基于字符串中每个字符的 Unicode 值。

更详细:

这是字典顺序的定义。如果两个字符串是
不同,那么
要么它们在某个索引处具有不同的字符,该索引对于两者来说都是有效的索引
字符串,或者它们的长度不同,或者两者都有。如果他们有不同的性格
在一个或多个索引位置处,令 k 为最小的此类索引;然后是字符串
位置 k 处的字符具有较小的值,这是通过使用 < 确定的。操作员,
按字典顺序位于另一个字符串之前。在这种情况下,compareTo 返回
两个字符串中位置k处的两个字符值的差...

from the javadocs:

The comparison is based on the Unicode value of each character in the strings.

more detailed:

This is the definition of lexicographic ordering. If two strings are
different, then
either they have different characters at some index that is a valid index for both
strings, or their lengths are different, or both. If they have different characters
at one or more index positions, let k be the smallest such index; then the string whose
character at position k has the smaller value, as determined by using the < operator,
lexicographically precedes the other string. In this case, compareTo returns the
difference of the two character values at position k in the two string ...

挽清梦 2024-12-19 03:49:01

希望这有帮助!

员工根据分数降序排序,如果两个不同的员工得分相同,那么我们需要考虑员工姓名按字典顺序排序。

Employee 类实现:(本例使用 Comparable 接口。)

@Override
public int compareTo(Object obj) {
    Employee emp = (Employee) obj;

    if(emp.getScore() > this.score) return 1;
    else if(emp.getScore() < this.score) return -1;
    else
        return emp.getEmpName().compareToIgnoreCase(this.empName) * -1;
}

Hope this helps!!

Employee sorted based on the descending order of the score and if two different employee has same score, then we need to consider Employee name for sorting lexicographically.

Employee class implementation: (Used Comparable interface for this case.)

@Override
public int compareTo(Object obj) {
    Employee emp = (Employee) obj;

    if(emp.getScore() > this.score) return 1;
    else if(emp.getScore() < this.score) return -1;
    else
        return emp.getEmpName().compareToIgnoreCase(this.empName) * -1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文