Java ArrayList sort() Method with Example

In the previous article we have discussed about Java ArrayList toArray() Method with Example

In this article we are going to see the use Java ArrayList sort() method along with suitable examples.

Java ArrayList sort() Method with Example

sort():

This java.util.ArrayList.sort() method is used to arrange the elements in ascending or descending order within the arraylist. If the arrayList contains both small letter and capital letter then it sorts first capital letter and then to small letter (in case of ascending order). If the list contains null element, then it sorts the null element 1st. (in case of ascending order) When we compare null values it throws NullPointerException.

Let’s see sort() method with suitable examples.

Method-1: Java ArrayList sort(comparator.naturalOrder()) Method

Syntax:

arrayListName.sort(comparator.naturalOrder())

Where,

  • arrayListName refers to the name of your ArrayList.
  • sort(comparator.naturalOrder()) refers to sort the elements of the arrayList in ascending order using a comparator which will compare the elements inside the ArrayList.

Approach:

  • Create a new ArrayList of type String.
  • Add string elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Then sort the elements in ascending order within the arrayList using sort(comparator.naturalOrder()) method.
  • Print the new ArrayList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create an ArrayList of string datatype
        ArrayList<String> arr = new ArrayList<String>();
        // Adding some elements to the ArrayList
        arr.add("A");
        arr.add("b");
        arr.add("D");
        arr.add("e");
        arr.add("F");
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // sorting the elements in ascending order
        arr.sort(Comparator.naturalOrder());
        // Prints the new ArrayList elements
        System.out.println("The sorted elements of ArrayList are: "+arr);
    }
}
Output:

The elements of ArrayList are: [A, b, D, e, F]
The sorted elements of ArrayList are: [A, D, F, b, e]

Method-2: Java ArrayList sort(comparator.reverseOrder()) Method

Syntax:

arrayListName.sort(comparator.reverseOrder())

Where,

  • arrayListName refers to the name of your ArrayList.
  • sort(comparator.reverseOrder()) refers to sort the elements of the arrayList in descending order using a comparator which will compare the elements inside the ArrayList.

Approach:

  • Create a new ArrayList of type Integer.
  • Add Integer elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Then sort the elements in ascending order within the arrayList using sort(comparator.reverseOrder()) method.
  • Print the new ArrayList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {        
        // Create an ArrayList of string datatype
        ArrayList<Integer> arr = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr.add(2);
        arr.add(52);
        arr.add(13);
        arr.add(17);
        arr.add(1);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // sorting the elements in descending order
        arr.sort(Comparator.reverseOrder());
        // Prints the new ArrayList elements
        System.out.println("The sorted elements of ArrayList are: "+arr);
    }
}
Output:

The elements of ArrayList are: [2, 52, 13, 17, 1]
The sorted elements of ArrayList are: [52, 17, 13, 2, 1]

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: