In this tutorial, you will learn how to sort ArrayList in Java. We will see java programs in this guide, to sort an ArrayList in ascending and descending order.
What is an ArrayList?
Arraylist is a class in java that implements List interface. It is based on an Array data structure. It is widely used because of the functionality and flexibility it offers. Most of the developers choose Arraylist over Array as ArrayList can dynamically grow and shrink after addition and removal of elements.
This is how an ArrayList looks like:
[Shyam, Chaitanya, Steve, Ram]
After sorting this ArrayList in ascending order:
[Chaitanya, Ram, Shyam, Steve]
After sorting the original ArrayList in descending order:
[Steve, Shyam, Ram, Chaitanya]
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:
Example 2: Sorting ArrayList of Integer Type
In this example, we have an ArrayList of integers and we are sorting it using the sort() method. This program is similar to the above program except that the list is of integer type here so the ArrayList elements are 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 just have to pass the Collection.reverseOrder()
as a second parameter in the Collections.sort()
method as shown below. Similarly, 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:
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:
Related guides:
papajo says
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?
Chaitanya Singh says
Refer this: https://beginnersbook.com/2014/10/how-to-remove-repeated-elements-from-arraylist/
Frank says
For the main method it is supposed to be (String[] args) not (String args[])
Chaitanya Singh says
Hello Frank, Both are same.
Gopal says
Great tutorial, even for beginners. Sorted my array immediately.
ni says
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?