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 Java

Last Updated: June 1, 2024 by Chaitanya Singh | Filed Under: java

In this tutorial, you will learn how to sort ArrayList in Java. We will write several java programs to accomplish this. We can use Collections.sort() method to sort an ArrayList in ascending and descending order.

//Sorting in Ascending order
ArrayList<Integer> numbers = new ArrayList<>(List.of(4, 1, 3, 2));
Collections.sort(numbers); // Sort the ArrayList in ascending order
System.out.println(numbers); // Output: [1, 2, 3, 4]

//Sorting in Descending order
ArrayList<Integer> numbers = new ArrayList<>(List.of(4, 1, 3, 2));
// Sort the ArrayList in descending order
Collections.sort(numbers, Collections.reverseOrder());
System.out.println(numbers); // Output: [4, 3, 2, 1]

Let’s see the complete programs:

Example 1: Sorting ArrayList in Ascending order

In this example, we have an ArrayList of String type. We are sorting the given ArrayList in ascending order using Collections.sort() method. Since this ArrayList is of string type, the elements are sorted in ascending alphabetical order.

import java.util.*;
public class JavaExample {
  public static void main(String args[]){
    ArrayList<String> listOfCountries = new ArrayList<>();
    listOfCountries.add("India");
    listOfCountries.add("US");
    listOfCountries.add("China");
    listOfCountries.add("Denmark");

    // Original unsorted list
    System.out.println("Before Sorting: "+ listOfCountries);

    //Sorting the ArrayList using sort() method
    Collections.sort(listOfCountries);

    // Printing sorted ArrayList
    System.out.println("After Sorting: "+ listOfCountries);
  }
}

Output:

Sort ArrayList in Java

Example 2: Sorting ArrayList of Integer Type

Let’s see the same program but with different type of ArrayList. Here, we are sorting an ArrayList of integers.

import java.util.*;
public class JavaExample{

  public static void main(String args[]){
    ArrayList<Integer> arraylist = new ArrayList<>();
    arraylist.add(11);
    arraylist.add(2);
    arraylist.add(7);
    arraylist.add(3);

    //Before sorting
    System.out.println("Before Sorting: "+ arraylist);

    // Sorting the list of integers using sort() method
    Collections.sort(arraylist);

    //After sorting
    System.out.println("After Sorting: "+ arraylist);
  }
}

Output:

Before Sorting: [11, 2, 7, 3]
After Sorting: [2, 3, 7, 11]

Example 3: Sorting an ArrayList in Descending order

In this program, we are sorting the given ArrayList in descending order. To sort an ArrayList in descending order, we need to pass Collection.reverseOrder() as a second parameter in the Collections.sort() method as shown below. The same way, we can sort an ArrayList of integer type in descending order as well.

import java.util.*;
public class JavaExample{

  public static void main(String args[]){
    ArrayList<String> arraylist = new ArrayList<>();
    arraylist.add("Apple");
    arraylist.add("Orange");
    arraylist.add("Kiwi");
    arraylist.add("Banana");
    arraylist.add("Mango");

    //Before sorting
    System.out.println("Before Sorting: "+ arraylist);

    // Sorting the list in descending order
    Collections.sort(arraylist, Collections.reverseOrder());

    //After sorting
    System.out.println("After Sorting: "+ arraylist);
  }
}

Output:

Sorting an ArrayList in Descending order

Example 4: Sort ArrayList entered by user in descending order

In this example, user is asked to enter the size of ArrayList. This is read and stored in an int variable size. A while loop runs to read and add the user entered elements to the ArrayList. Once the list is populated with the user entered elements, the list is sorted in reverse order by using the sort() method similar to the Example 3.

import java.util.*;
public class JavaExample {
  public static void main(String[] args) {
    int i=0, size;
    Scanner scan = new Scanner(System.in);
    ArrayList<String> list = new ArrayList<>();

    System.out.println("How many elements you want in list? ");
    size = scan.nextInt();
    Scanner scan2 = new Scanner(System.in);
    System.out.println("Enter elements: ");
    while(i<size)
    {
      list.add(scan2.nextLine());
      i++;
    }

    //Before sorting
    System.out.println("Before Sorting: "+ list);

    // Sorting the list in descending order
    Collections.sort(list, Collections.reverseOrder());

    //After sorting
    System.out.println("After Sorting: "+ list);
  }
}

Output:

Sort ArrayList Descending order user input


Related guides:

  1. Sort ArrayList in descending order
  2. Sort ArrayList of Objects using Comparable and Comparator
❮ Java ArrayListJava Collections ❯

Top Related Articles:

  1. Multilevel inheritance in java with example
  2. How to Convert an array to ArrayList in java
  3. Java 8 features with examples
  4. ArrayList in Java With Examples
  5. Convert ArrayList to Array in Java

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. papajo says

    December 17, 2014 at 5:53 PM

    Hi, good article, but will Collections.sort eliminate duplicates in case the list contains any duplicates? If not can you show how to eliminate the duplicates?

    Reply
    • Chaitanya Singh says

      December 19, 2014 at 5:09 AM

      Refer this: https://beginnersbook.com/2014/10/how-to-remove-repeated-elements-from-arraylist/

      Reply
  2. Frank says

    July 6, 2015 at 3:22 PM

    For the main method it is supposed to be (String[] args) not (String args[])

    Reply
    • Chaitanya Singh says

      July 10, 2015 at 1:32 PM

      Hello Frank, Both are same.

      Reply
  3. Gopal says

    August 29, 2015 at 9:46 AM

    Great tutorial, even for beginners. Sorted my array immediately.

    Reply
  4. ni says

    July 10, 2017 at 3:23 PM

    One question, does .sort retain existing order? This is often relevant. If you have it sorted by one criteria, then sort it by another criteria, do values by the second criteria which are equal still show the initial sort criteria is retained, or is the resulting order random?

    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