List sort descending Java
By using Collections.reverseOrder(Comparator Show Let's see a simple example to sort the ArrayList in descending order. SmartPhone.java import java.util.Comparator;
public class SmartPhone {
String brand;
String model;
intprice;
intrating;
SmartPhone(String brand,String model,intprice, intrating){
this.brand = brand;
this.model = model;
this.price = price;
this.rating = rating;
}
public String getBrand() {
returnbrand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
returnmodel;
}
public void setModel(String model) {
this.model = model;
}
public int getPrice() {
returnprice;
}
public void setPrice(intprice) {
this.price = price;
}
public int getRating() {
returnrating;
}
public void setRating(intrating) {
this.rating = rating;
}
public String toString() {
return"SmartPhone [brand=" + brand + ", model=" + model + ", price=" + price + ", rating=" + rating + "]";
}
public int compareTo(SmartPhone sp) {
returnthis.price - sp.price;
}
}
class RatingComparator implements Comparator ArrayListLearning.java public class ArrayListLearning {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List Output: Actual List [SmartPhone [brand=Apple, model=6s, price=50000, rating=10], SmartPhone [brand=lg, model=pro2, price=40000, rating=9], SmartPhone [brand=MI, model=3s, price=10000, rating=6], SmartPhone [brand=Letv, model=le2, price=12000, rating=7]] Sorting the list as comparator [SmartPhone [brand=MI, model=3s, price=10000, rating=6], SmartPhone [brand=Letv, model=le2, price=12000, rating=7], SmartPhone [brand=lg, model=pro2, price=40000, rating=9], SmartPhone [brand=Apple, model=6s, price=50000, rating=10]] Reversing the Comparator sorting Printing the reverse list [SmartPhone [brand=Apple, model=6s, price=50000, rating=10], SmartPhone [brand=lg, model=pro2, price=40000, rating=9], SmartPhone [brand=Letv, model=le2, price=12000, rating=7], SmartPhone [brand=MI, model=3s, price=10000, rating=6]] Next TopicJava Collections Interview Questions
I have already shared sorting of ArrayList in ascending order. In this post we will learn how to sort an ArrayList in descending order (decreasing order). Read Also : How to Synchronize ArrayList in Java with Example Example of Sorting ArrayList in Descending order To sort the ArrayList in descending order we will use two methods Collections.reverseOrder() method and Collections.sort() method. One liner will be like : Collections.sort(arraylist, Collections.reverseOrder()); Another way of sorting ArrayList in Descending order is to sort the list in ascending order first and then it will be reversed. Collections.sort(list); Collections.reverse(list); Code of Sorting ArrayList in Descending Order import java.util.*; public class ArrayListDescendingSort { public static void main(String args[]) { ArrayList<String> arraylist = new ArrayList<String>(); arraylist.add("Apple"); arraylist.add("Banana"); arraylist.add("Pear"); arraylist.add("Mango"); /*Unsorted List: ArrayList content before sorting*/ System.out.println("ArrayList Before Sorting:"); for(String str: arraylist){ System.out.println(str); } /* Sorting in decreasing (descending) order*/ Collections.sort(arraylist, Collections.reverseOrder()); /* Sorted List in reverse order*/ System.out.println("ArrayList in descending order:"); for(String str: arraylist){ System.out.println(str); } } } Output ArrayList Before Sorting: Apple Banana Pear Mango ArrayList in descending order: Pear Mango Banana Apple Above example is to sort String ArrayList. Similarly, we can sort ArrayList of Integers in descending order. import java.util.*; public class ArrayListDescendingSort2 { public static void main(String args[]) { ArrayList<Integer> arraylist = new ArrayList<Integer>(); arraylist.add(1); arraylist.add(13); arraylist.add(89); arraylist.add(45); /*Unsorted List: ArrayList content before sorting*/ System.out.println("ArrayList Before Sorting:"); for(int num: arraylist){ System.out.println(num); } /* Sorting in decreasing (descending) order*/ Collections.sort(arraylist, Collections.reverseOrder()); /* Sorted List in reverse order*/ System.out.println("ArrayList in descending order:"); for(int num: arraylist){ System.out.println(num); } } } Output ArrayList Before Sorting: 1 13 89 45 ArrayList in descending order: 89 45 13 1 A quick guide on how to sort the arraylist in descending order or reverse order in java and example programs using Collections.reverseOrder() method. 1. OverviewIn this article, We will learn how to sort ArrayList in descending order in java. Sometimes this is referred as collections reverse or decreasing order. To get the list in the reverse order, we need to use Collections.reverseOrder() and Collections.sort() methods together. 2. Sort ArrayList In Descending order using Collections.reverseOrder()We have already shown how to sort list in ascending order using Collections.sort() method. In the below examples, we are using the built-in comparator from the reverseOrder() method and passing it to the Collections.sort() method.
Output:
3. Sort ArrayList In Descending order using Collections.reverse()Next, look at the another way to sort the arraylist in descending order using two methods as below. Collections.sort(arraylist); –> first sorts the list in the ascending order Collections.reverse(arraylist); –> Next, reverse the sorted list. Example:
This program also produces the same output as in the section 2. 4. Java 8 Sort ArrayList Descending OrderSorting list in reverse order is pretty easy from stream.sorted(Collections.reverseOrder()) in java 8 api. We can use parallelStream() method to work efficiently with larger data volumes.
5. ConclusionIn this article, We have seen how to sort the ArrayList in descending order in older and new java 8 streams. GitHub How to sort Map by values in java 8? |