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

前端 未结 4 1830

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 to

4条回答
  •  不要未来只要你来
    2021-02-01 02:27

    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.

    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.

    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.

提交回复
热议问题