Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
The Basics of Comparator
Creating a User-Defined Class
Crafting a Generic Comparator
Sorting with Comparator
Frequently Asked Questions
What is a Comparator in Java?
What does a Generic Comparator do?
How is the compare method used in the Comparator interface?
Last Updated: Mar 27, 2024

Generic Comparator in Java

Author Gunjan Batra
0 upvote
Crack Google SDE interview : Essential projects
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM


When working with Java, Sorting is an essential operation that every developer needs to understand. Thankfully, Java provides robust tools and interfaces to make this task easier. One such tool is the Comparator interface. 

Generic Comparator in java

This article will explore how we can create a generic Comparator to compare objects of a user-defined class in Java.

The Basics of Comparator

The Comparator interface in Java is used for sorting collections of objects. It allows you to order objects based on user-defined conditions, adding a level of customization to the sorting process. A typical Comparator interface might look like this:

Comparator<T> {
    int compare(T o1, T o2);

The compare method takes two objects for comparison and returns an integer. If the return value is negative, o1 is less than o2. If it's zero, both are equal. If it's positive, o1 is greater than o2.

Creating a User-Defined Class

Let's create a user-defined class Student which we'll later sort using our generic comparator.

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

    // constructor, getters and setters

Crafting a Generic Comparator

A generic comparator is a flexible tool that allows sorting by different attributes of a user-defined class. Let's create a generic comparator StudentComparator for our Student class.

public class StudentComparator implements Comparator<Student> {
    public int compare(Student s1, Student s2) {
        return s1.getName().compareTo(s2.getName());

Here, the StudentComparator compares Student objects based on their names. You can change this comparison criterion according to your requirements.

Sorting with Comparator

Now that we have a comparator, we can use it to sort a list of students:

List<Student> students = // initialization
Collections.sort(students, new StudentComparator());

The above line will sort the students list based on the students' names in alphabetical order.

Also see,  Eclipse ide for Java Developers

Also Read, addition of two numbers in java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Frequently Asked Questions

What is a Comparator in Java?

A Comparator in Java is an interface used to sort collections of objects based on user-defined conditions.

What does a Generic Comparator do?

A Generic Comparator allows you to compare objects of a user-defined class based on different attributes.

How is the compare method used in the Comparator interface?

The compare method takes two objects and returns an integer indicating whether the first object is less than, equal to, or greater than the second object.


In Java, the Comparator interface provides a powerful way to sort collections of objects. A generic comparator adds flexibility, enabling sorting by different attributes of a user-defined class. By implementing the compare method, you dictate the sorting logic, which could be anything from alphabetical order to numerical order. This flexibility makes the Comparator interface a vital tool in every Java programmer's toolkit. Whether you're a beginner or a seasoned developer, understanding how to use and create generic comparators can enhance your programming skills and allow you to write more adaptable code.

For more information, refer to our Guided Path on Coding Ninjas Studio to upskill yourself in PythonData Structures and Algorithms, Competitive ProgrammingSystem Design, and many more! 

Head over to our practice platform, Coding Ninjas Studio, to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more! 

Happy Learning Ninja!

Previous article
Java List
Next article
Volatile Keyword in Java
Live masterclass