Sort ArrayList in Descending Order Using Comparator in Java Last Updated : 13 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report A comparator is an interface that is used to rearrange the ArrayList in a sorted manner. A comparator is used to sort an ArrayList of User-defined objects. In java, a Comparator is provided in java.util package. Using Comparator sort ArrayList on the basis of multiple variables, or simply implement Comparator without affecting the original User-defined class. To sort an ArrayList using Comparator override the compare() method provided by the comparator interface. Override the compare() method in such a way that it will reorder an ArrayList in descending order instead of ascending order. Change only in the comparison part for descending order. See the difference between both compare() functions. Ascending Order public int compare(Item i1, Item i2) { if (i1.Property== i2.Property) return 0; else if (i1.Property > i2.Property) return 1; else return -1; } Descending Order public int compare(Item i1, Item i2) { if (i1.Property== i2.Property) return 0; else if (i1.Property < i2.Property) return 1; else return -1; }If there is any need to reorder an ArrayList on the basis of the variable of string type like name, etc. rewrite the compare() function Like below. Ascending Order public int compare(Shop s1, Shop s2) { return s1.name.compareTo(s2.name); } Descending Order public int compare(Shop s1, Shop s2) { return s2.name.compareTo(s1.name); }Example: Java // Java Program to sort the ArrayList // in descending order using comparator import java.util.*; // create the Laptop class class Laptop { int ModalNo; String name; int ram; Laptop(int ModalNo, String name, int ram) { this.ModalNo = ModalNo; this.name = name; this.ram = ram; } } // creates the comparator for comparing RAM class RamComparator implements Comparator<Laptop> { // override the compare() method public int compare(Laptop l1, Laptop l2) { if (l1.ram == l2.ram) { return 0; } else if (l1.ram < l2.ram) { return 1; } else { return -1; } } } class GFG { public static void main(String[] args) { // create the ArrayList object ArrayList<Laptop> l = new ArrayList<Laptop>(); l.add(new Laptop(322, "Dell", 2)); l.add(new Laptop(342, "Asus", 8)); l.add(new Laptop(821, "HP", 16)); l.add(new Laptop(251, "Lenovo", 6)); l.add(new Laptop(572, "Acer", 4)); System.out.println("before sorting"); System.out.println("Ram" + " " + "Name" + " " + "ModalNo"); for (Laptop laptop : l) { System.out.println(laptop.ram + " " + laptop.name + " " + laptop.ModalNo); } System.out.println(); System.out.println("After sorting(sorted by Ram)"); System.out.println("Ram" + " " + "Name" + " " + "ModalNo"); // call the sort function Collections.sort(l, new RamComparator()); for (Laptop laptop : l) { System.out.println(laptop.ram + " " + laptop.name + " " + laptop.ModalNo); } } } Outputbefore sorting Ram Name ModalNo 2 Dell 322 8 Asus 342 16 HP 821 6 Lenovo 251 4 Acer 572 After sorting(sorted by Ram) Ram Name ModalNo 16 HP 821 8 Asus 342 6 Lenovo 251 4 Acer 572 2 Dell 322 Comment More infoAdvertise with us Next Article Sort an array of pairs using Java Arrays.sort() with custom Comparator M meetsuvariya Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-ArrayList Java-Comparator +2 More Practice Tags : Java Similar Reads Sort Java Vector in Descending Order Using Comparator The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in java.util package and implements the List interface, so we can use all the methods of the List interface. There are two types of Sorting t 3 min read How to Sort ArrayList using Comparator? Comparator is an interface that is used for rearranging the Arraylist in a sorted manner. Comparator is used to sort an ArrayList of User-defined objects. In java, Comparator is provided in java.util package. Using Comparator we can sort ArrayList on the basis of multiple variables. We can simply im 3 min read How to Change the Comparator to Return a Descending Order in Java TreeSet? Comparator interface is used to order the objects of user-defined classes. A comparator object is capable of comparing two objects of two different classes. TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the eleme 3 min read Creating TreeSet with Comparator by User Define Objects in Java TreeSet is the implementation class of Set Interface. It follows a natural sorting order or you can customize it using a comparator and it also does not allow duplicates. Syntax: TreeSet<String> gfg= new TreeSet<>(); Below is the normal implementation of the TreeSet: Java // Java program 6 min read Sort an array of pairs using Java Arrays.sort() with custom Comparator Given an array of pairs of integers. The task is to sort the array with respect to the second element of the pair.Example:Input: [(10, 20), (20, 30), (5, 6), (2, 5)] Output: [(2, 5), (5, 6), (10, 20), (20, 30)]Program of Java Array Sort Custom ComparatorJava// Java code to sort the array // accordin 2 min read Java Program to Sort the Elements of an Array in Descending Order Here, we will sort the array in descending order to arrange elements from largest to smallest. The simple solution is to use Collections.reverseOrder() method. Another way is sorting in ascending order and reversing.1. Using Collections.reverseOrder()In this example, we will use Collections.reverseO 2 min read Like