Comparator and Comparable (Sorting) in Java

Vivek Singh
3 min readJun 1, 2021

--

GitHub repo link for Comparator and Comparable implementations : https://github.com/Viveksingh1313/sortingCustomClasses

Both the interface is used to sort an array of similar objects. Calling Collections.sort(customClassObject) will result in a compilation error, because JDK is unable to understand the type of Object you have passed as parameter. Hence to sort a custom defined class/object we need to implement Comparator or Comparable interfaces.

Difference b/w the two :

In Comparator we create multiple separate classes to compare based on the sorting element. In Comparable, only one class will be modified for sorting, and that will be your POJO class.

When to use Comparable ?

Use Comparable when you are sure that sorting is to be done based on a single element like age or marks.

When do we go for Comparator ?

When the sorting needs to be done based on different elements based on different requirements. For one use case you may need sorting based on marks, while for other use case you may need to sort based on year.

Program logic (Comparable):

// A Java program to demonstrate use of Comparable
import java.io.*;
import java.util.*;
//POJO class
class Student implements Comparable<Student> {

private int marks;
private String name;
private int year;

public int compareTo(Student m)
{
return this.marks - m.marks; // sort based on marks

//To sort based on year uncomment below line
// return (this.marks - m.marks);
}

// Constructor
public Student(String name, int marks, int year)
{
this.name = name;
this.marks = marks;
this.year = year;
}

// Getter methods for accessing private data
public int getMarks() { return marks; }
public String getName() { return name; }
public int getYear() { return year; }
}

// Main class
public class ComparableLogic
{
public static void main(String[] args)
{
ArrayList<Student> studentList = new ArrayList<Student>();
studentList.add(new Student("John Mayer", 89, 2015));
studentList.add(new Student("Doja Cat", 87, 1977));
studentList.add(new Student("Eric Clapton", 85, 1980));
studentList.add(new Student("Andrew NG", 86, 1983));

Collections.sort(studentList);

System.out.println("Students after sorting based on marks : ");
for (Student student: studentList)
{
System.out.println(student.getName() + " " +
student.getMarks() + " " +
student.getYear());
}
}
}

//Output :
// Students after sorting based on marks :
// Eric Clapton 85 1980
// Andrew NG 86 1983
// Doja Cat 87 1977
// John Mayer 89 2015

Program Logic(Comparator) :

// A Java program to demonstrate use of Comparable
import java.io.*;
import java.util.*;
//Pojo class
class Student {
private int marks;
private String name;
private int year;

// Constructor
public Student(String name, int marks, int year)
{
this.name = name;
this.marks = marks;
this.year = year;
}

// Getter methods for accessing private data
public int getMarks() { return marks; }
public String getName() { return name; }
public int getYear() { return year; }
}
// Sorting Class ByYear
class SortByYear implements Comparator<Student> {

public int compare(Student student1, Student student2) {
return student1.getYear() - student2.getYear();

}

}

// Sorting class ByMarks
class SortByMarks implements Comparator<Student> {

public int compare(Student student1, Student student2) {
return student1.getMarks() - student2.getMarks();
}

}

// Main class
public class ComparatorLogic
{
public static void main(String[] args)
{
ArrayList<Student> studentList = new ArrayList<Student>();
studentList.add(new Student("John Mayer", 89, 2015));
studentList.add(new Student("Doja Cat", 87, 1977));
studentList.add(new Student("Eric Clapton", 85, 1980));
studentList.add(new Student("Andrew NG", 86, 1983));
Collections.sort(studentList, new SortByMarks());

System.out.println("Students after sorting based on age: ");
for (Student student: studentList)
{
System.out.println(student.getName() + " " +
student.getMarks() + " " +
student.getYear());
}
Collections.sort(studentList, new SortByYear());

System.out.println("Students after sorting based on year : ");
for (Student student: studentList)
{
System.out.println(student.getName() + " " +
student.getMarks() + " " +
student.getYear());
}
}
}

//
//Output :
// Students after sorting based on age:
// Eric Clapton 85 1980
// Andrew NG 86 1983
// Doja Cat 87 1977
// John Mayer 89 2015
// Students after sorting based on year :
// Doja Cat 87 1977
// Eric Clapton 85 1980
// Andrew NG 86 1983
// John Mayer 89 2015

You can check this blog if you want to do the same implementation in Java8 using Lambdas : https://www.baeldung.com/java-8-sort-lambda

Conclusion

Using Comparable we can use only one comparison whereas we can write more than one custom implementation using Comparator. Like in the comparable example we could just sort by only one attribute, i.e., year or marks but in the comparator, we were able to use different attributes like year and marks both depending on our usage.

--

--

Vivek Singh
Vivek Singh

Written by Vivek Singh

Software Developer. I write about Full Stack, NLP and Blockchain. Buy me a coffee - buymeacoffee.com/viveksinless

No responses yet