Summary: In this programming example, we will learn to sort a list of objects based on one of their properties.

Example:

Input:
Object{ roll_no: 77, name: "Parshant" }
Object{ roll_no: 41, name: "Anuj" }
Object{ roll_no: 56, name: "Badal" }

//sort by roll_no
Output:  
Object{ roll_no: 41, name: "Anuj" }
Object{ roll_no: 56, name: "Badal" }
Object{ roll_no: 77, name: "Parshant" }

In Java, to sort objects based on their properties, we use a comparable or comparator interface.

Let’s see how can we use them to sort an list of objects.

Method 1: Using Comparable Interface

Java has an inbuilt Comparable interface, which when implemented by a class, its objects could be compared with each other.

When we implement a comparable interface, we need to define the logic of comparison from within the compareTo() method in the same class.

The logic defined in the compareTo() method determines which of the two objects should come first in order

Tip: return -ve number from the compareTo() method if this should appear before the object passed as an argument otherwise, return +ve number.

Here is an example in Java that sorts an list of objects using the Comparable interface:

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
 
class Student implements Comparable<Student>{
  int roll_no;
  String name;
 
  Student(int roll_no, String name){
    this.roll_no = roll_no;
    this.name = name;
  }
 
  public String toString(){
    return "{ roll_no: "+roll_no+", name: "+name+" }";
  }
 
  //override the compareTo() method of Comparable interface.
  public int compareTo(Student std2){
    return this.roll_no - std2.roll_no;
  }
}
 
class Main {
  public static void main(String[] args) {
    List<Student> students = new ArrayList<>();
    
    students.add(new Student(41, "Anuj"));
    students.add(new Student(77, "Parshant"));
    students.add(new Student(56, "Badal"));
 
    System.out.println("Before Sorting: ");
    for(Student x: students){
      System.out.println(x);
    }
 
    //sorting arraylist using the Collections.sort() method
    Collections.sort(students);
 
    System.out.println("After Sorting: ");
    for(Student x: students){
      System.out.println(x);
    }
    
  }
}

Output:

Before Sorting:
{ roll_no: 41, name: Anuj }
{ roll_no: 77, name: Parshant }
{ roll_no: 56, name: Badal }

After Sorting:
{ roll_no: 41, name: Anuj }
{ roll_no: 56, name: Badal }
{ roll_no: 77, name: Parshant }

Method 2: Using Comparator Object

The Collections.sort() method in Java also accepts an instance of the Comparator class as an second argument (optional).

In this case, the compare()method of the Comparator class contains the logic of comparison.

The method accepts two objects as arguments and returns an integer after successful comparison.

Tip: return -ve number, if first of the two objects (passed as arguments) should appear before the second object in the order otherwise, return +ve number.

Here is the Java program that sorts an list of objects using the Comparator class:

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

class Student{
  int roll_no;
  String name;

  Student(int roll_no, String name){
    this.roll_no = roll_no;
    this.name = name;
  }

  public String toString(){
    return "{ roll_no: "+roll_no+", name: "+name+" }";
  }

  //static comparator object
  public static Comparator<Student> rollComparator = new Comparator<>(){
    //logic of comparision
    public int compare(Student std1, Student std2){
      return std1.roll_no - std2.roll_no;
    }
  };

}

class Main {
  public static void main(String[] args) {

    //define list of objects
    List<Student> students = new ArrayList<>();
    students.add(new Student(41, "Anuj"));
    students.add(new Student(37, "Parshant"));
    students.add(new Student(56, "Badal"));

    System.out.println("Before Sorting: ");
    for(Student x: students){
      System.out.println(x);
    }

    //sort list using the Collections.sort() method
    //also, pass the comparator object reference as 2nd argument
    Collections.sort(students, Student.rollComparator);

    System.out.println(" \nSorting by roll_no: ");
    for(Student x: students){
      System.out.println(x);
    }
    
  }
}

Output:

Before Sorting:
{ roll_no: 41, name: Anuj }
{ roll_no: 37, name: Parshant }
{ roll_no: 56, name: Badal }

Sorting by roll_no:
{ roll_no: 37, name: Parshant }
{ roll_no: 41, name: Anuj }
{ roll_no: 56, name: Badal }

Leave a Reply