List sort descending Java

By using Collections.reverseOrder[Comparatorcmp] method, we can sort the collection in reverse order. The reverseOrder[] method does the reversing on the basis of given Comparator. In case of null, it will reverse collection in natural ordering.

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 { @Override public int compare[SmartPhone obj1, SmartPhone obj2] { return [obj1.ratingobj2.rating] ? 1 : 0; } }

ArrayListLearning.java

public class ArrayListLearning { @SuppressWarnings["unchecked"] public static void main[String[] args] { List phoneList = new ArrayList[]; SmartPhone ph1 = new SmartPhone["Apple", "6s", 50000, 10]; SmartPhone ph2 = new SmartPhone["lg", "pro2", 40000, 9]; SmartPhone ph3 = new SmartPhone["MI", "3s", 10000, 6]; SmartPhone ph4 = new SmartPhone["Letv", "le2", 12000, 7]; phoneList.add[ph1]; phoneList.add[ph2]; phoneList.add[ph3]; phoneList.add[ph4]; System.out.println["Actual List"]; System.out.println[phoneList]; System.out.println["Sorting the list as comparator"]; Collections.sort[phoneList, new RatingComparator[]]; System.out.println[phoneList]; System.out.println["Reversing the Comparator sorting"]; Comparator cmp = Collections.reverseOrder[new RatingComparator[]]; Collections.sort[phoneList, cmp]; System.out.println["Printing the reverse list"]; System.out.println[phoneList]; } }

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 arraylist = new ArrayList[]; 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 arraylist = new ArrayList[]; 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. Overview

In 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.

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

package com.javaprogramto.java8.arraylist;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

public class ArrayListReverseOrder1 {

    public static void main[String[] args] {

        List numbersList = new ArrayList[];

        numbersList.add[150];

        numbersList.add[50];

        numbersList.add[250];

        numbersList.add[500];

        numbersList.add[350];

        System.out.println["Before sorting : " + numbersList];

        Comparator reverseComparator = Collections.reverseOrder[];

        Collections.sort[numbersList, reverseComparator];

        System.out.println["After sorting : " + numbersList];

    }

}

Output:

1

2

Before sorting : [150, 50, 250, 500, 350]

After sorting : [500, 350, 250, 150, 50]

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:

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class ArrayListReverseOrder2 {

    public static void main[String[] args] {

        List numbersList = new ArrayList[];

        numbersList.add[150];

        numbersList.add[50];

        numbersList.add[250];

        numbersList.add[500];

        numbersList.add[350];

        System.out.println["Before sorting : " + numbersList];

        Collections.sort[numbersList];

        Collections.reverse[numbersList];

        System.out.println["After sorting : " + numbersList];

    }

}

This program also produces the same output as in the section 2.

4. Java 8 Sort ArrayList Descending Order

Sorting 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.

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.stream.Collectors;

public class ArrayListReverseOrder2 {

    public static void main[String[] args] {

        List numbersList = new ArrayList[];

        numbersList.add[150];

        numbersList.add[50];

        numbersList.add[250];

        numbersList.add[500];

        numbersList.add[350];

        System.out.println["Before sorting : " + numbersList];

        List descendingList = numbersList.stream[]

                .sorted[Collections.reverseOrder[]]

                .collect[Collectors.toList[]];

        System.out.println["After sorting : " + descendingList];

    }

}

5. Conclusion

In 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?

Video liên quan

Chủ Đề