How to Sort ArrayList in Java

More often we need to sort an ArrayList. Following example shows you how to sort an ArrayList.

Sort String or Wrapper classes for primitive data types

List of String or wrapper classes for primitive data types can be sorted using Collections.sort(values) method. This method sorts values in natural order as described below.

	List<string> values = new ArrayList<string>();
	values.add("Atul");
	values.add("Vrajesh");
	values.add("Tirth");
	values.add("Kirti");
	values.add("Shlok");
	values.add("Dhruv");

	System.out.println("Unsorted String values:");
	System.out.println("=======================");
	for (String value : values) {
		System.out.println(value);
	}

	// Sort values
	Collections.sort(values);

	System.out.println();
	System.out.println("Sorted String values:");
	System.out.println("=====================");
	for (String value : values) {
		System.out.println(value);
	}

Sort ArrayList values in reverse (i.e. descending) order

ArrayList values can be sorted in descending order using Collections.reverseOrder() as described below.

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

Sort Java Objects using Comparable interface

To sort objects by its property value it is required to implement Comparable interface and override compareTo() method. This way object become comparable with each other.
Comparable should be used if objects are suppose to be sorted in natural way and most of the time the class is generally expected to be sorted in similar manner. Refer the following Employee class.

package com.elitejavacoder;

import java.util.Comparator;

public class Employee implements Comparable<Employee> {
	private Long id;
	private String name;

	public Employee(Long id, String name) {
			this.id = id;
			this.name = name;
	}

	public Long getId() {
			return id;
	}

	public void setId(Long id) {
			this.id = id;
	}

	public String getName() {
			return name;
	}

	public void setName(String name) {
			this.name = name;
	}

	@Override
	public int compareTo(Employee anotherEmployee) {
			return this.getId().compareTo(anotherEmployee.getId()); // Default sorting on employee id
	}
}

ArrayList of employees can be sorted as shown in the following example code.

	List<Employee> employees = new ArrayList<Employee>();
	employees.add(new Employee(10L, "Atul"));
	employees.add(new Employee(1L, "Vrajesh"));
	employees.add(new Employee(7L, "Tirth"));
	employees.add(new Employee(9L, "Kirti"));
	employees.add(new Employee(2L, "Shlok"));

	System.out.println("Unsorted Employees:");
	System.out.println("===================");
	for (Employee employee : employees) {
		System.out.println("ID: " + employee.getId() + " Name: " + employee.getName());
	}

	// Sort Objects
	Collections.sort(employees);

	System.out.println();
	System.out.println("Sorted Employees:");
	System.out.println("=================");
	for (Employee employee : employees) {
		System.out.println("ID: " + employee.getId() + " Name: " + employee.getName());
	}

Sort Java Objects using Comparator interface

Comparator is better option if the sorting only makes sense for that specific use case. Refer the following sample comparator to sort employees by their name.

	class EmployeeNameComparator implements Comparator<Employee> {

		@Override
		public int compare(Employee employee1, Employee employee2) {
			return employee1.getName().compareTo(employee2.getName());
		}
	}

Refer following code that shows you how to use Comparator.

	// Sort Employees by name
	Collections.sort(employees, new EmployeeNameComparator());

	System.out.println("Sorted Employees on Name:");
	System.out.println("=========================");
	for (Employee employee : employees) {
		System.out.println("ID: " + employee.getId() + " Name: " + employee.getName());
	}

	// Sort Employees by name (Reverse)
	Collections.sort(employees, Collections.reverseOrder(new EmployeeNameComparator()));

	System.out.println();
	System.out.println("Sorted Employees on Name:");
	System.out.println("=========================");
	for (Employee employee : employees) {
		System.out.println("ID: " + employee.getId() + " Name: " + employee.getName());
	}