beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How to sort ArrayList in descending order in Java

By Chaitanya Singh | Filed Under: Java Collections

Earlier we shared the examples of ArrayList sorting in ascending order. Here we will learn how to sort an ArrayList in descending (or decreasing) order.

Example: Sorting in Descending order

We are using Collections.reverseOrder() method along with Collections.sort() in order to sort the list in decreasing order. In the below example we have used the following statement for sorting in reverse order.
Collections.sort(arraylist, Collections.reverseOrder());

However the reverse order sorting can also be done as following – This way the list will be sorted in ascending order first and then it will be reversed.
Collections.sort(list);
Collections.reverse(list);

Complete example:

import java.util.*;
public class Details  {

	public static void main(String args[]){
	   ArrayList<String> arraylist = new ArrayList<String>();
	   arraylist.add("AA");
	   arraylist.add("ZZ");
	   arraylist.add("CC");
	   arraylist.add("FF");

	   /*Unsorted List: ArrayList content before sorting*/
	   System.out.println("Before Sorting:");
	   for(String str: arraylist){
			System.out.println(str);
		}

	   /* Sorting in decreasing 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:

Before Sorting:
AA
ZZ
CC
FF
ArrayList in descending order:
ZZ
FF
CC
AA

In the above example we have used the ArrayList of String type (ArrayList<String>) for sorting. The same sorting method can be used for the list of integers as well.

Enjoyed this post? Try these related posts

  1. Java – Convert a LinkedList to ArrayList
  2. How to serialize HashMap in java
  3. How to get the Sub Map from TreeMap example – Java
  4. How to get sublist of an ArrayList with example
  5. Java ArrayList addAll(int index, Collection c) Method example
  6. Java – Convert Vector to ArrayList example

Comments

  1. Amisha says

    September 30, 2014 at 9:36 AM

    Though Collections is one of the most interesting part of java, was not satisfied with any sites I have visited so far. Must say, the collection section of your website is just a wonderful door to understanding the collections in the most subtle way. Thanks a lot :) Each and every section is just very nicely explained. :)

    Reply
  2. Hassan Jamil says

    July 27, 2015 at 1:00 PM

    Very helpful, saved my time.

    Reply
  3. Dipak Rai says

    January 17, 2016 at 6:47 PM

    Thank You, Thumbs Up!

    Reply

Leave a Reply Cancel reply

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

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap