使用collections.sort()方法对字符串进行排序

时间:2021-03-27 17:13:37

As per the documentation: This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array

根据文档:此实现将指定的列表转储到数组中,对数组进行排序,并迭代列表,从数组中的相应位置重置每个元素

Given the program below, I am not able to understand the sorting as how internally jvm judges that letter 'A' is smaller or bigger than letter 'a'? As this is a string, the letters won't be assumed in ascii value so how the sorting happens?

鉴于下面的程序,我无法理解排序是因为内部jvm判断字母“A”是小于还是大于字母“a”?由于这是一个字符串,字母不会在ascii值中假定,所以排序是如何发生的?

public class LetterASort {
    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList();
        strings.add("aAaA");
        strings.add("AaA");
        strings.add("aAa");
        strings.add("AAaa");
        Collections.sort(strings);
        for (String s : strings) 
        { 
        System.out.print(s + " "); //prints AAaa AaA aAa aAaA 
        }
    }
}

Also I tried debugging the code which created a new doubt for me: the length of array turned out to be 4 rather than 3 as collections.sort is included in the length

我也尝试调试代码,这给我带来了新的疑问:数组的长度变成4而不是3,因为collections.sort包含在长度中

2 个解决方案

#1


5  

The "natural ordering" that Collections.sort refers to is the one specified by Comparable -- which String implements, and which defines only one method, compareTo. So, the answer is in the definition of String.compareTo. Its documentation states:

Collections.sort引用的“自然顺序”是由Comparable指定的 - 它实现的,并且只定义了一个方法compareTo。所以,答案是在String.compareTo的定义中。其文件说明:

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

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

Lexicographical ordering basically means dictionary ordering. Essentially, order each letter alphabetically as far as you go, but if you're still tied when either word runs out of letters, then the shorter word goes first.

词典排序基本上意味着字典排序。基本上,按照字母顺序对每个字母进行排序,但是如果在任何一个单词用完字母时仍然被绑定,则较短的单词首先出现。

Unicode is the numerical value that each character has. There's a great introductory post about it here (it's not short, but it does a good job of walking you through not just what unicode is, but why it exists).

Unicode是每个字符具有的数值。这里有一篇很棒的介绍性帖子(它并不简短,但是它很好地指导你完成unicode是什么,但为什么它存在)。

#2


3  

String class implements the Comparable interface. When sort happens, compareTo(String) method is called. For more look at the implementation of compareTo(String) method in String class.

String类实现Comparable接口。排序发生时,调用compareTo(String)方法。有关String类中compareTo(String)方法的实现的更多信息。

#1


5  

The "natural ordering" that Collections.sort refers to is the one specified by Comparable -- which String implements, and which defines only one method, compareTo. So, the answer is in the definition of String.compareTo. Its documentation states:

Collections.sort引用的“自然顺序”是由Comparable指定的 - 它实现的,并且只定义了一个方法compareTo。所以,答案是在String.compareTo的定义中。其文件说明:

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

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

Lexicographical ordering basically means dictionary ordering. Essentially, order each letter alphabetically as far as you go, but if you're still tied when either word runs out of letters, then the shorter word goes first.

词典排序基本上意味着字典排序。基本上,按照字母顺序对每个字母进行排序,但是如果在任何一个单词用完字母时仍然被绑定,则较短的单词首先出现。

Unicode is the numerical value that each character has. There's a great introductory post about it here (it's not short, but it does a good job of walking you through not just what unicode is, but why it exists).

Unicode是每个字符具有的数值。这里有一篇很棒的介绍性帖子(它并不简短,但是它很好地指导你完成unicode是什么,但为什么它存在)。

#2


3  

String class implements the Comparable interface. When sort happens, compareTo(String) method is called. For more look at the implementation of compareTo(String) method in String class.

String类实现Comparable接口。排序发生时,调用compareTo(String)方法。有关String类中compareTo(String)方法的实现的更多信息。