BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to sort ArrayList in descending order in Java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial, you will learn how to sort an ArrayList in descending order.

Example 1: Sorting an ArrayList in Descending order

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

You can also sort an ArrayList in descending order like this. This way the list will be sorted in ascending order first and then it will be reversed.

Collections.sort(list);
Collections.reverse(list);

Source code:
In this example, we have an ArrayList of string type. We have added few elements to it using add() method, we are sorting the ArrayList using Collections.sort(). In the end, we are printing the sorted ArrayList.

import java.util.*;
public class JavaExample  {

  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 sorted the ArrayList of String type (ArrayList<String>), the logic is same for other type of ArrayList as well.

Example 2: Sorting an ArrayList in descending order using Collections.reverse()

import java.util.*;
public class JavaExample  {

  public static void main(String args[]){
    ArrayList<String> arrList = new ArrayList<String>();
    arrList.add("Chaitanya");
    arrList.add("BeginnersBook");
    arrList.add("Tutorials");
    arrList.add("Website");

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

    // Sorting in decreasing order
    Collections.sort(arrList); //sorting in ascending order
    Collections.reverse(arrList); //reversing the sorted list

    // print sorted arraylist
    System.out.println();//new line
    System.out.println("After sorting: ");
    for(String str: arrList){
      System.out.println(str);
    }
  }
}

Output:

Before Sorting:
Chaitanya
BeginnersBook
Tutorials
Website

After sorting: 
Website
Tutorials
Chaitanya
BeginnersBook

Recommended Articles:

  • Sorting ArrayList of Objects
  • Swap two elements in an ArrayList
  • Empty an ArrayList in Java
  • Override toString() method for ArrayList
❮ Java Collections

Top Related Articles:

  1. How to Convert an array to ArrayList in java
  2. Multilevel inheritance in java with example
  3. Add Multiple Items to an ArrayList in Java
  4. How to loop HashMap in java
  5. Java 8 features with examples

Tags: Collections, Java-ArrayList

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

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 Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap