How to sort an ArrayList in Java
In this post, you will learn how to sort an ArrayList using the Java programming language.
ArrayList in Java
The ArrayList class implements the List interface. It uses a dynamic array to store the duplicate elements of different data types. ArrayList is a part of the collection framework and is present in the java.util package. It is like an array, but there is no size limit.
ArrayList is a dynamic array, and we do not have to specify the size while creating it. We can add or remove elements at any time. ArrayList is not synchronized, which means multiple threads can work on ArrayList at the same time. It provides some useful methods like add(), addAll(), capacity(), clear(), clone(), elements(), equals(), size(), set(), remove(), and trimToSize().
Java sort an ArrayList using the Collections.sort() Method
Java collections class provides the Collections.sort(List) method to sort the linked list, array, queue, and other data structures. In this post, we use this method to sort an ArrayList in ascending order. It takes the elements of ArrayList as parameters and sorts them in ascending order, and then returns an ArrayList that is sorted in ascending order.
Syntax -Collections.sort(ArrayList);
Example 1-
// Java program to Sort ArrayList in Ascending Order
// using sort() method of collection class
// Import the Java util class
import java.util.*;
public class SortArrayList{
public static void main(String args[]){
// Creating object of ArrayList class called list
ArrayList list = new ArrayList();
// Adding elements to the ArrayList
list.add(55);
list.add(10);
list.add(62);
list.add(42);
list.add(86);
// Displaying the unsorted ArrayList
System.out.println("Before Sorting: "+ list);
// Sorting ArrayList in Ascending Order
Collections.sort(list);
// Displaying the sorted ArrayList
System.out.println("After Sorting: "+ list);
}
}
Output of the above code:
Before Sorting: [55, 10, 62, 42, 86]
After Sorting: [10, 42, 55, 62, 86]
Example 2-
// Java program to Sort ArrayList in Ascending Order
// using sort() method of collection class
// Import the Java util class
import java.util.*;
public class SortArrayList{
public static void main(String args[]){
// Creating object of ArrayList class called bicycle
ArrayList<String> bicycle = new ArrayList<String>();
// Adding elements to the ArrayList
bicycle.add("Decathlon");
bicycle.add("Firefox");
bicycle.add("Montra");
bicycle.add("Hercules");
bicycle.add("Cradiac");
bicycle.add("Avon");
bicycle.add("Hero");
// Displaying the unsorted ArrayList
System.out.println("Before Sorting: "+ bicycle);
// Sorting ArrayList in Ascending Order
Collections.sort(bicycle);
// Displaying the sorted ArrayList
System.out.println("After Sorting: "+ bicycle);
}
}
Output of the above code:
Before Sorting: [Decathlon, Firefox, Montra, Hercules, Cradiac, Avon, Hero]
After Sorting: [Avon, Cradiac, Decathlon, Firefox, Hercules, Hero, Montra]
Java sort an ArrayList using Comparator attribute
In the given Java program, we use the sort() method to sort the list of elements by values by passing a customized comparator. It sorts the list elements by comparing the ASCII values of the elements.
Syntax-sort(ArrayList list, Comparator <T>)
It sorts the elements of the list according to the order included by the comparator.
Example-// Java program to Sort ArrayList in Ascending Order
// using sort() method
// Import the Java util class
import java.util.*;
public class SortArrayList{
public static void main(String args[]){
// Creating object of ArrayList class called list
ArrayList list = new ArrayList();
// Adding elements to the ArrayList
list.add(89);
list.add(20);
list.add(64);
list.add(98);
list.add(39);
// Displaying the unsorted ArrayList
System.out.println("Before Sorting: "+ list);
// Sorting ArrayList in Ascending Order
list.sort(Comparator.naturalOrder());
// Displaying the sorted ArrayList
System.out.println("After Sorting: "+ list);
}
}
Output of the above code:
Before Sorting: [89, 20, 64, 98, 39]
After Sorting: [20, 39, 64, 89, 98]
In the above code, the naturalOrder() method of Comparator Interface returns a comparator that use to compare Comparable objects in natural order.
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number