Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. It is not. The comparator compares between 2 elements. 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?

enter image description here

like image 616
AlikElzin-kilaka Avatar asked Jul 14 '13 17:07

AlikElzin-kilaka


People also ask

Does Collections sort go smallest to largest?

The default sorting order for an object is ascending order like Integer will be sorted from low to high while descending order is just opposite. Collections. reverseOrder() returns a Comparator which will be used for sorting Objects in descending order.

In what order does the Collections sort method arrange a list of strings?

sort() method is present in java. util. Collections class. It is used to sort the elements present in the specified list of Collection in ascending order.

Does Collections sort sort ascending or descending?

By default, Collection. sort performs the sorting in ascending order. If we want to sort the elements in reverse order we could use following methods: reverseOrder() : Returns a Comparator that imposes the reverse of natural ordering of elements of the collection.

Which sorting does Collections sort use?

Collections sort is a method of Java Collections class used to sort a list, which implements the List interface. All the elements in the list must be mutually comparable. If a list consists of string elements, then it will be sorted in alphabetical order.

How to sort a list of collection in Java?

java.util.Collections.sort() method is present in java.util.Collections class. It is used to sort the elements present in the specified list of Collection in ascending order. It works similar to java.util.Arrays.sort() method but it is better then as it can sort the elements of Array as well as linked list, queue and many more present in it.

What is the use of sort in Java?

The java.Collections.sort () is very similar to the java.util.Arrays.sort () method; rather it is much better than it. The java.Collections.sort () method is also used to sort the linked list, array, queue, and other data structures.

How do you sort a list by comparator?

The sort () method of List Interface sorts the given list according to the order specified in the comparator. The list must be modifiable else it will throw an exception. The parameter 'c' represents the Comparator used to compare list elements.

How to sort list of employee objects by name in Java?

Checkout for comparison logic in compareTo () method. Nest Java program sorts the list of Employee objects by their name; 2. Custom Sorting using Comparators The second parameter in sort () method takes an instance of Comparator.


1 Answers

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):

Sorts the specified list according to the order induced by the specified comparator.

From the documentation for 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.

like image 51
Andy Thomas Avatar answered Sep 28 '22 05:09

Andy Thomas