How to Sort ArrayList of Objects in Java

In this tutorial you will learn how to sort arraylist of objects in java.

In our previous tutorial we have seen how to sort arraylist of String or Integer type using Collection.sort() method.

Can we use that process to sort an arraylist of custom objects? The answer is big no.

For doing this we have to use Comparable or Comparator interfaces.

Below I have shared an example for each of them.

How to Sort ArrayList of Objects in Java
Image Source

Sort ArrayList of Objects Using Comparable

Comparable is used to sort an arraylist of object on the basis of single property at a time.

For example we have student data in which we have two properties, name and age. We can sort it by name or age.

This can be done by implementing Comparable interface and overriding its compareTo() method.

 

Student.java

package com;

public class Student implements Comparable<Student>{
	private String name;
	private int age;

	public Student(String name,int age){
		this.name = name;
		this.age = age;
	}
		
	@Override
	public int compareTo(Student obj) {
		
		//sort in ascending order
		return this.age-obj.age;
		
		//sort in descending order
		//return obj.age-this.age;
	}
	
	//this method is used to print the Student object in user friendly manner
	@Override
	public String toString() {
		return "Name:"+name+", Age:"+age;
	}
}

 

SortObjectArrayList.java

package com;

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

public class SortObjectArrayList {
	public static void main(String args[]){
		ArrayList<Student> al = new ArrayList<Student>();
		
		al.add(new Student("Neeraj", 22));
		al.add(new Student("Pawan", 27));
		al.add(new Student("Pankaj", 25));
		al.add(new Student("Suraj", 28));
		
		Collections.sort(al);
		
		for(Student s:al){
			System.out.println(s);
		}
	}
}

 

Output

Name:Neeraj, Age:22
Name:Pankaj, Age:25
Name:Pawan, Age:27
Name:Suraj, Age:28

 

Sort ArrayList of Objects Using Comparator

We can sort student data on the basis of multiple properties like name and age by overriding compare() method of Comparator interface.

 

Student.class

package com;

import java.util.Comparator;

public class Student{
	private String name;
	private int age;

	public Student(String name,int age){
		this.name = name;
		this.age = age;
	}
	
	//this method is used to print the Student object in user friendly manner
	@Override
	public String toString() {
		return "Name:"+name+", Age:"+age;
	}
	
	//sort by name
	public static Comparator<Student> sortByName = new Comparator<Student>() {
		
		@Override
		public int compare(Student obj1, Student obj2) {
			
			//sort in ascending order
			return obj1.name.compareTo(obj2.name);
			
			//sort in descending order
			//return obj2.name.compareTo(obj1.name);
		}
	}; 
	
	//sort by age
	public static Comparator<Student> sortByAge = new Comparator<Student>() {
		
		@Override
		public int compare(Student obj1, Student obj2) {
			
			//sort in ascending order
			return obj1.age-obj2.age;
			
			//sort in descending order
			//return obj2.age-obj1.age;
		}
	}; 
}

 

SortObjectArrayList.java

package com;

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

public class SortObjectArrayList {
	public static void main(String args[]){
		ArrayList<Student> al = new ArrayList<Student>();
		
		al.add(new Student("Neeraj", 22));
		al.add(new Student("Pawan", 27));
		al.add(new Student("Pankaj", 25));
		al.add(new Student("Suraj", 19));
		
		Collections.sort(al,Student.sortByName);
		
		System.out.println("Sort by Name:");
		for(Student s:al){
			System.out.println(s);
		}
		
		Collections.sort(al,Student.sortByAge);
		
		System.out.println("\nSort by Age:");
		for(Student s:al){
			System.out.println(s);
		}

	}
}

 

Output

Sort by Name:
Name:Neeraj, Age:22
Name:Pankaj, Age:25
Name:Pawan, Age:27
Name:Suraj, Age:19

Sort by Age:
Name:Suraj, Age:19
Name:Neeraj, Age:22
Name:Pankaj, Age:25
Name:Pawan, Age:27

 

The above code is self explanatory, comment below if still you are facing and difficulty.

1 thought on “How to Sort ArrayList of Objects in Java”

  1. Admin,

    Thanks for opening this blog and for the updating it with relevant topics and prompt responses. I have learnt a whole lot from your postings.
    I have bookmark this site. God bless

Leave a Comment

Your email address will not be published. Required fields are marked *