Home » Java » How to sort ArrayList in Java

How to sort ArrayList in Java

To sort an ArrayList object, use Collection.sort method. This is a static method and it sorts an ArrayList object's elements into ascending order.

We have given two example of sorting an String ArrayList and Integer ArrayList.

Example 1: Sorting of ArrayList<String> :

In this example we are sorting the ArrayList of String type.simply call Collections.sort(arraylist) method. The output List will be sorted alphabetically.


package com.example;

import java.util.ArrayList;
import java.util.Collections;

public class ListSorting {

	public static void main(String args[]) {
		ArrayList<String> list = new ArrayList<String>();
		list.add("Ravi");
		list.add("Amit");
		list.add("Neil");
		list.add("Aayush");

		System.out.println("Before Sorting list element is :");
		for (String str : list) {
			System.out.println(str);
		}

		Collections.sort(list);

		/* List after sorting */
		System.out.println("After Sorting list is:");
		for (String str : list) {
			System.out.println(str);
		}
	}
}

Output :

Before Sorting list element is :
Ravi
Amit
Neil
Aayush
After Sorting list is:
Aayush
Amit
Neil
Ravi

Example 2: Sorting of ArrayList <integer> :

We can use same Collection.sort method to sort Integer ArrayList.


package com.example;

import java.util.ArrayList;
import java.util.Collections;

public class ListSorting {

	public static void main(String args[]) {
		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(23);
		list.add(35);
		list.add(10);
		list.add(45);

		System.out.println("Before Sorting list element is :");
		for (int value : list) {
			System.out.println(value);
		}

		Collections.sort(list);

		/* List after sorting */
		System.out.println("After Sorting list is:");
		for (int value : list) {
			System.out.println(value);
		}
	}
}

Output :

Before Sorting list element is :
23
35
10
45
After Sorting list is:
10
23
35
45

How to sort ArrayList in descending order in Java :

in order to sort the list in decreasing order use Collections.reverseOrder() method along with Collections.sort(). In the below example we have used.

Collections.sort(arraylist, Collections.reverseOrder());

However the reverse order sorting can also be done by sorting list in ascending order first and then it will be reversed.

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

Example :


package com.example;

import java.util.ArrayList;
import java.util.Collections;

public class ListSorting {

	public static void main(String args[]) {
		ArrayList<String> arraylist = new ArrayList<String>();
		arraylist.add("A");
		arraylist.add("Z");
		arraylist.add("B");
		arraylist.add("G");

		/* ArrayList Element before sorting */
		System.out.println("Before Sorting Element is:");
		for (String str : arraylist) {
			System.out.println(str);
		}

		/* Sorting list in decreasing order */
		Collections.sort(arraylist, Collections.reverseOrder());
		System.out.println("ArrayList element in descending order:");
		for (String str : arraylist) {
			System.out.println(str);
		}
	}
}

Output :

Before Sorting Element is:
A
Z
B
G
ArrayList element in descending order:
Z
G
B
A

Previous Next Article

comments powered by Disqus