简体   繁体   English

Java的Collections.sort(列表,比较器)的排序顺序是什么?从小到大还是从大到小?

[英]What's the sort order of Java's Collections.sort(list, comparator)? small to big or big to small?

Apparently, it's not documented or I missed it. 显然,它没有记录,或者我错过了它。

Here 's the link to the documentation and below's the text as an image: 是文档的链接,下面是文本作为图像:

EDIT (17/5): I think too many confused this question to be a comparator question. 编辑 (17/5):我认为太多人把这个问题混淆为比较问题。 It is not. 它不是。 The comparator compares between 2 elements. 比较器比较2个元素。 According to that comparison, the list sorted. 根据那个比较,列表排序。 How? 怎么样? Ascending or Descending? 升序还是降序?

I'll refine/simplify the question even further: If the comparator decides that element A is smaller than element B. In the sorted list , will element A be located at a lower index than element B? 我将进一步细化/简化问题:如果比较器确定元素A小于元素B. 在排序列表中 ,元素A是否位于比元素B更低的索引处?

在此输入图像描述

The sort order is always ascending , where the Comparator defines which items are larger than others. 排序顺序始终是升序 ,比较器定义哪些项比其他项大。

From the documentation for Collections.sort(List<T> list, Comparator<? super T> c) : Collections.sort(List <T>列表,Comparator <?super T> c)的文档中:

Sorts the specified list according to the order induced by the specified comparator. 根据指定比较器引发的顺序对指定列表进行排序。

From the documentation for Comparator.compare(T,T) : Comparator.compare(T,T)的文档:

Compares its two arguments for order. 比较它的两个参数的顺序。 Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. 返回负整数,零或正整数,因为第一个参数小于,等于或大于第二个参数。

You (or rather, your comparator) decides. 你(或者更确切地说,你的比较器)决定。

  • If your Comparator 's compare(T o1, T o2) return a negative when o1 is less than o2 , you get ascending order ( demo on ideone ). 如果Comparatorcompare(T o1, T o2)o1小于o2时返回负数,则升序( 在ideone上演示 )。
  • If your Comparator 's compare(T o1, T o2) return a negative when o1 is greater than o2 , you get descending order ( demo on ideone ). 如果Comparatorcompare(T o1, T o2)o1大于o2时返回负数,则降序( 在ideone上演示 )。

Another way of saying the same thing would be that sort assumes that the comparator orders the two items passed into it from smaller ( o1 ) to greater ( o2 ), and produces an ascending sort consistent with that ordering. 说同样的事情将是另一种方式sort假定比较下令传递到它从较小(这两个项目o1 )更大( o2 ),并产生一个上升与排序排序是一致的。

The documentation of Comparator.compareTo(o1, o2) method says Comparator.compareTo(o1, o2)方法的文档说

Compares its two arguments for order. 比较它的两个参数的顺序。 Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. 返回负整数,零或正整数,因为第一个参数小于,等于或大于第二个参数。

So if you want to sort from natural ordering , that is small to big, then you should write the implementation as defined in the documentation 因此,如果您想从自然排序(从小到大)排序,那么您应该编写文档中定义的实现

public int compareTo(Integer o1, Integer o2) {
     int v1 = (o1);
     int v2 = (o2);
     if(v1 == v2) {
        return 0;
     }
     if(v1 < v2) {
        return -1; //return negative integer if first argument is less than second
     }
     return 1;
}

If you want the sorting to be in reverse order, that is big to small 如果您希望排序顺序相反,则从大到小

public int compareTo(Integer o1, Integer o2) {
     int v1 = (o1);
     int v2 = (o2);
     if(v1 == v2) {
        return 0;
     }
     if(v1 < v2) {
        return 1;  //do the other way
     }
     return -1;
}

According to the documentation https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator , the sort implementation for Collections.sort(list, comparator) is mergeSort. 根据文档https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator) ,集合的排序实现.sort(list,comparator)是mergeSort。

Given the result produced by mergeSort is ascending ( https://en.wikipedia.org/wiki/Merge_sort ), the sort order of Collections.sort(list, comparator) is ascending. 鉴于mergeSort生成的结果是升序( https://en.wikipedia.org/wiki/Merge_sort ),Collections.sort(列表,比较器)的排序顺序是升序。

That is to say if the comparator decides that element A is smaller than element B. In the sorted list, element A will be located at a smaller index than element B. 也就是说,如果比较器确定元素A小于元素B.在排序列表中,元素A将位于比元素B小的索引处。

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

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