Java中的词典顺序 [英] Lexicographic Order in Java

查看:130
本文介绍了Java中的词典顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中定义字典顺序,特别是在引用特殊字符时,例如等等?

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

可以在这里找到示例性订单

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

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.

推荐答案

来自 String.compareTo


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

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


这是词典排序的定义。如果两个字符串不同,则它们在某个索引处具有不同的字符,这些字符串是两个字符串的有效索引,或者它们的长度不同,或者两者都有。如果它们在一个或多个索引位置具有不同的字符,则令k为最小的索引;然后,通过使用<来确定位置k处的字符具有较小值的字符串。运算符,按字典顺序排在另一个字符串之前。在这种情况下,compareTo返回两个字符串中位置k处两个字符值的差异[...]

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 [...]

所以基本上,它将每个字符串视为一系列16位无符号整数。没有文化意识,不了解复合角色等。如果你想要更复杂的排序,你应该看看 Collat​​or

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中的词典顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆