Java 中的字典顺序
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自
String.compareTo
:和
因此基本上,它将每个字符串视为 16 位无符号整数序列。没有文化意识,不理解复合字符等。如果您想要更复杂的排序,您应该查看
Collator
。From the docs for
String.compareTo
:and
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
.在 Java 中,它基于字符串的 Unicode 值:
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#compareTo(java.lang.String)
在Oracle中,这取决于在您正在使用的字符集上您的数据库将希望它是 UTF-8,以便与 Java 具有一致的行为。
要检查字符集:
如果它不是 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:
If it's not UTF-8, then you can get different comparison behavior depending on which character set your Oracle database is using.
来自 javadocs :
更详细:
from the javadocs:
more detailed:
希望这有帮助!
员工根据分数降序排序,如果两个不同的员工得分相同,那么我们需要考虑员工姓名按字典顺序排序。
Employee 类实现:(本例使用 Comparable 接口。)
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.)