Comparator.reversed() Method Example in Java 8

What I can do If I want to sort a list of Employee with a reverse comparator. We get a new default method reversed() method on Comparator Interface, that will take the same comparator but in reverse order.

See our Comparator.comparing() method code example, in which we created a Comparator new java 8 way, which will do sort Employees by last name property. After performing the sorting logic I simply want to reverse the sorting result by last name property field using new Comparator Interface default reverse() method.

The output of the programme is:

In this example first we sort the Employee by last name field and then reverse the existing Comparator empComparatorByLastName sorting logic created on last name property field.

 

Leave a Reply

Your email address will not be published. Required fields are marked *