Javatpoint Logo
Javatpoint Logo

Collections Sort in Java 8

In sorting of Collections in Java 8, Lambda Expression and Collections interface place an important role. There are various ways through which we can sort a list using Java 8 Lambda Expression. But Collections interface itself provides some sorting methods through which we can easily sort the collection object in Java 8.

A list can be sorted by using several sorting methods provided by the stream, Comparator, and Collections interfaces. In this section, we will discuss the methods provided by the Collections interface. If you want to learn all the methods of sorting provided by any class, click here.

Collections interface provides the following two sorting methods to sort the elements of the list:

  1. sort()
  2. reverseOrder()
Collections Sort in Java 8

1) sort()

The Collections.sort() method is available in the java.util.Collections class. The Collections.sort() method helps us to sort the elements available in the specified list of Collections. By default, the sort() method sorts the elements in ascending order.

The sort() method of the Collections interface works similarly to the Arrays interface. The sort method of the Collections interface can sort not only a list but also linked list, queue and many more that are available in the Collections, and that makes the Collection's sort method better than Arrays.

Syntax

Parameters

1) type

It defines the type of the parameter.

2) list

It defines the list which we need to sort using this method.

Let's take an example to understand how it sorts the list in Java 8

SortExample.java

Output:

Collections Sort in Java 8

Explanation

In the above code, we create one ArrayList of type string and one LinkedList of type integer. Both lists have certain values in random order. We first print both the list without performing the sorting, and after that, we print both the list after performing the sorting using the Collections.sort() method.

2) reverseOrder()

The Collections.reverseOrder() is a method which returns a Comparator. The resulting Comparator applies the reverse of the natural ordering on the objects. Collections provide two different types of Java reverseOrder() method based on the parameter. These methods are as follows:

  1. Java Collections reverseOrder() Method
  2. Java Collections reverseOrder(comp) Method

Collections.reverseOrder()

The reverseOrder() method of Collections is used to derive the Comparator. The resulting Comparator applies the inverse of the natural ordering on a collection of objects. It implements the comparable interface.

Syntax

Parameters

It doesn't have any parameters.

Let's take an example to understand how it sorts the list in Java 8

ReverseOrderExample1.java

Output:

Collections Sort in Java 8

Explanation

In the above code, we create a linked list of type integer and add certain elements to it. After adding elements, we print that linked list, and then we sort it using Collections sort() and reverseOrder() methods. We use the reverseOrder() method to perform sorting in descending order. At the end, we print the sorted linked list.

Collections.reverseOrder(comp)

The reverseOrder(comp) method of Collections is used to derive the Comparator. The resulting Comparator applies the inverse ordering of the specified Comparator.

Syntax

Parameters

comp

It defines the Comparator whose ordering we want to be inversed or reversed by the returned Comparator.

Let's take an example to understand how the reverseOrder(comp) is used to sort the list in Java 8

ReverseOrderExample2.java

Output:

Collections Sort in Java 8

Explanation

In the above code, we create an Employee class having property emp_id, name, and address. In the constructor, we set the values for these properties. The toString() method returns the employee's detail to print it in the main method.

We create a Comparator class SortByEmpId that implements the Comparator interface. This interface has an abstract method, i.e., compare(), which we need to define. So, we create a compare method that returns the difference b/w the id of the emp1 and emp2.

In the main class, we create an array list having details of four employees and print them using the for loop. After that, we get the Comparator using the Collections's reverseOrder() method and using that Comparator; we sort the employees in descending order of employee id. At last, we print the sorted array list of the employee.

Conclusion

The Collections interface provide only one method to perform sorting, i.e., sort() method. It performs sorting in ascending order. The Collections's reverseOrder() method helps the sort() method to perform sorting in descending order by returning the Comparator.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA