Java Comparator.reverseOrder
March 08, 2019
Comparator.reverseOrder
is a static method introduced in Java 8, that returns Comparator
to imposes sorting in reverse natural ordering of collection of objects. For natural ordering, a class needs to implement Comparable
and define compareTo
method. A collection of objects are sorted according to compareTo
in natural ordering. Comparator.reverseOrder
reverses the natural ordering. It calls Collections.reverseOrder()
internally and returns Comparator
instance. Find the Java source code for Comparator.reverseOrder
.
public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() { return Collections.reverseOrder(); }
Comparator
also provides a method as Comparator.naturalOrder
that imposes natural ordering on collection of objects. The natural order for number is numeric order, string is sorted according to alphabetic order and dates are sorted according to chronological order. Java classes such as Integer
, String
and Date
implement Comparable
and override its compareTo
method for natural ordering. To facilitate natural ordering in our classes, we need to implement Comparable
interface and define compareTo
method.
Comparator.reverseOrder
returns Comparator
to reverse natural ordering. We can use Comparator.reverseOrder
with Stream.sorted
, List.sort
, Collections.sort
and Arrays.sort
to sort the collections of objects in reverse natural order.
Contents
Comparator.reverseOrder with Stream.sorted
Stream.sorted
returns a stream consisting of the elements of this stream, sorted according to the provided comparator.
StreamSortedDemo.java
package com.concretepage; import java.util.Arrays; import java.util.Comparator; import java.util.List; public class StreamSortedDemo { public static void main(String[] args) { List<Integer> numList = Arrays.asList(12, 10, 15, 8, 11); numList.stream().sorted(Comparator.reverseOrder()).forEach(n -> System.out.print(n + " ")); System.out.println("\n-----------"); List<String> strList = Arrays.asList("Varanasi", "Allahabad", "Kanpur", "Noida"); strList.stream().sorted(Comparator.reverseOrder()).forEach(s -> System.out.print(s + " ")); System.out.println("\n-----------"); List<Student> stdList = Student.getStudentList(); stdList.stream().sorted(Comparator.reverseOrder()).forEach(s -> System.out.print(s.getName() + " ")); } }
package com.concretepage; import java.util.Arrays; import java.util.List; public class Student implements Comparable<Student> { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public int compareTo(Student s) { return name.compareTo(s.getName()); } public static List<Student> getStudentList() { Student s1 = new Student("Ram", 18); Student s2 = new Student("Shyam", 22); Student s3 = new Student("Mohan", 19); Student s4 = new Student("Mahesh", 20); Student s5 = new Student("Krishna", 21); List<Student> list = Arrays.asList(s1, s2, s3, s4, s5); return list; } }
15 12 11 10 8 ----------- Varanasi Noida Kanpur Allahabad ----------- Shyam Ram Mohan Mahesh Krishna
Comparator.reverseOrder with Collections.sort
Collections.sort
sorts the specified list according to the given Comparator instance.
CollectionsSortDemo.java
package com.concretepage; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class CollectionsSortDemo { public static void main(String[] args) { List<Integer> numList = Arrays.asList(12, 10, 15, 8, 11); Collections.sort(numList, Comparator.reverseOrder()); numList.forEach(n -> System.out.print(n + " ")); System.out.println("\n-----------"); List<String> strList = Arrays.asList("Varanasi", "Allahabad", "Kanpur", "Noida"); Collections.sort(strList, Comparator.reverseOrder()); strList.forEach(s -> System.out.print(s + " ")); System.out.println("\n-----------"); List<Student> stdList = Student.getStudentList(); Collections.sort(stdList, Comparator.reverseOrder()); stdList.forEach(s -> System.out.print(s.getName() + " ")); } }
Comparator.reverseOrder with List.sort
List.sort
sorts this list according to the given Comparator instance.
ListSortDemo.java
package com.concretepage; import java.util.Arrays; import java.util.Comparator; import java.util.List; public class ListSortDemo { public static void main(String[] args) { List<Integer> numList = Arrays.asList(12, 10, 15, 8, 11); numList.sort(Comparator.reverseOrder()); numList.forEach(n -> System.out.print(n + " ")); System.out.println("\n-----------"); List<String> strList = Arrays.asList("Varanasi", "Allahabad", "Kanpur", "Noida"); strList.sort(Comparator.reverseOrder()); strList.forEach(s -> System.out.print(s + " ")); System.out.println("\n-----------"); List<Student> stdList = Student.getStudentList(); stdList.sort(Comparator.reverseOrder()); stdList.forEach(s -> System.out.print(s.getName() + " ")); } }
Comparator.reverseOrder with Arrays.sort
Arrays.sort
sorts the specified array of objects according to the order induced by the specified comparator.
ArraysSortDemo.java
package com.concretepage; import java.util.Arrays; import java.util.Comparator; public class ArraysSortDemo { public static void main(String[] args) { Student s1 = new Student("Ram", 18); Student s2 = new Student("Shyam", 22); Student s3 = new Student("Mohan", 19); Student[] stdArray = { s1, s2, s3 }; Arrays.sort(stdArray, Comparator.reverseOrder()); for (Student s : stdArray) { System.out.print(s.getName() + " "); } } }
References
Interface ComparatorJava 8 Stream sorted() Example