In this write-up, we will discuss the below-listed ways to sort a list in Java:
- How to sort a list using Collections.sort() function?
- How to sort a list in Reverse order using Collection.reverseOrder() method?
- How to sort a list using stream.sorted() method?
- How to sort a list using Comparator.naturalOrder() method?
Let’s jump into the practical implementation of the above-mentioned sorting methods.
How to sort a list using Collections.sort() function?
It is a build-in method of “java.util.Collections” used to arrange/sort a list in ascending order (natural order). The Collections.sort() method is used to sort arrays, linked lists, queues, etc.
Firstly, we need to import the Collections class, Arrays class, and List class to sort a list using sort() method:
import java.util.Arrays;
import java.util.Collections;
Code
Collections.sort(numlist);
System.out.println("Sorted List: ");
for (Integer show : numlist) {
System.out.println(show);
}
In this coding example, firstly, we created an integer-type list using Arrays.asList() method. Next, we utilized the Collections.sort() method to sort the list. Finally, we utilized the for-Each loop to traverse and print each element of the sorted list:
Output
The output clearly showed that the Collections.sort() method sorted the list successfully.
How to sort a list in Reverse order using Collection.reverseOrder() method?
The reverseOrder() is a static method that belongs to the Collections class of the java.util package. In Java, the Collections.reverseOrder() method is used to arrange/sort a list in descending order.
Code
Collections.sort(numList, Collections.reverseOrder());
System.out.println("Sorted List: "+ numList);
In this example program, we utilized the Arrays.asList() method to get a list. Next, we utilized the sort() method that takes the original list and the reverseOrder() method as arguments. Consequently, it returned a sorted (descending order) list. Finally, we utilized the println() method to print the sorted list.
Output
The output clarified that the reverseOrder() method succeeded in printing the given list in descending order.
How to sort a list using stream.sorted() method?
Java’s stream.sorted() method is used to arrange the list elements in descending order. It is an inbuilt method of the “java.util.stream” interface.
Code:
List<Integer> resultantList = numlist.stream().sorted().collect(Collectors.toList());
System.out.println("Sorted List: ");
for (Integer show : resultantList) {
System.out.println(show);
}
In this program, we created a numeric list using the Arrays.asList() method. Afterward, we utilized the stream.sorted() method to sort the list’s elements. Next, we utilized the collect() method to collect the elements from a stream and kept them in a collection. Afterward, we utilized the toList() method of the Collectors class to get the entered elements in a list. Finally, we utilized the for-Each loop to traverse and print every element of the sorted list:
Output
The output verified the working of the stream.sorted() method.
How to sort a list using Comparator.naturalOrder() method?
In Java, the naturalOrder() is an inbuilt function of the Comparator interface. It returns a comparator that is used to compare the objects in natural/ascending order. In Java, the comparator returned by the naturalOrder() method is serializable. The naturalOrder() method will throw a NullPointerException when compared to the null.
Code:
numList.sort(Comparator.naturalOrder());
System.out.println("Sorted List: "+ numList);
In this coding example, firstly, we created a list and initialized it with some values. Afterward, we utilized the naturalOrder() method to sort the given list in natural/ascending order. Finally, we printed the sorted list using the System.out.println() statement:
Output:
The above output clearly shows that the naturalOrder() method sorted the given list in ascending(natural) order. Similarly, the Comparator.reverseOrder() method sorts the list in reverse(descending) order.
Conclusion
Java offers multiple methods to sort a list in ascending/descending order, such as the Collections.sort(), Collections.reverseOrder(), Comparator.naturalOrder() and so on. All these methods are used to sort a Java list; however, some are used to sort a list in ascending order while the others are used to sort the list in descending order. This write-up considered multiple examples to understand the concept of list sorting in a better way.