CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Comparator in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

Comparator in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 4 min read ยท 0 Comments
Share: in X

Comparator in Java is an interface that belongs to the java.util package. It’s designed to facilitate the comparison of objects, allowing you to create custom sorting rules. This becomes incredibly useful when dealing with complex objects or objects from external libraries that lack a natural order.

Table of Contents

  • Implementing the Comparator Interface
  • Sorting with Comparators
  • Using Lambda Expressions for Conciseness
  • Key Advantages of Using Comparators
  • Common Use Cases
    • Sorting a List of Custom Objects
    • Sorting in Reverse Order
  • Practical Example of Using Comparator in Java
  • FAQs
    • How is a Comparator different from Comparable?
    • Can I use multiple criteria for sorting using Comparators?
    • Is it possible to sort objects in a case-insensitive manner?
    • What happens if I compare two objects as equal in a Comparator?
    • Are there any performance considerations when using Comparators?
    • Can I use Comparators with arrays?

Implementing the Comparator Interface

To use a Comparator, you need to implement the compare() method defined in the interface. This method takes two objects as parameters and returns a negative integer, zero, or a positive integer based on whether the first object is less than, equal to, or greater than the second object, respectively.

Following is a basic implementation example:

import java.util.Comparator;

public class AgeComparator implements Comparator<Person> {
    @Override
    public int compare(Person person1, Person person2) {
        return person1.getAge() - person2.getAge();
    }
}

Sorting with Comparators

Once you’ve defined a Comparator, you can use it to sort a collection of objects. Java provides the Collections class and its sort() method to achieve this.

List<Person> people = new ArrayList<>();
// Add people to the list

Comparator<Person> ageComparator = new AgeComparator();
Collections.sort(people, ageComparator);

Using Lambda Expressions for Conciseness

Java 8 introduced lambda expressions, which make Comparator implementations more concise. The previous example can be rewritten using lambdas:

List<Person> people = new ArrayList<>();
// Add people to the list

Comparator<Person> ageComparator = (person1, person2) -> person1.getAge() - person2.getAge();
Collections.sort(people, ageComparator);

Key Advantages of Using Comparators

  • Flexibility: Comparators allow you to sort objects based on criteria that aren’t inherent to the objects themselves.
  • Customization: You can create multiple Comparators to sort objects differently without modifying their classes.
  • External Sorting: Comparators are particularly useful when dealing with objects from third-party libraries that you can’t modify.

Common Use Cases

Sorting a List of Custom Objects

Suppose you have a list of Product objects that you want to sort by price. By implementing a Comparator for the Product class, you can easily achieve this.

Sorting in Reverse Order

Comparators also allow you to sort objects in reverse order. This can be accomplished by simply reversing the comparison logic.

Practical Example of Using Comparator in Java

Following is an example of how we can use a Comparator in Java to sort a list of custom objects based on a specific criteria. You can copy and paste this code into your Java development environment to see it in action:

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

class Student {
    private String name;
    private int age;

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

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

public class Main {
    public static void main(String[] args) {
        // Create a list of Student objects
        List<Student> students = new ArrayList<>();
        students.add(new Student("Virat", 22));
        students.add(new Student("Sachin", 20));
        students.add(new Student("Yuvraj", 24));
        students.add(new Student("Ashish", 19));

        // Define a Comparator to sort by age in ascending order
        Comparator<Student> ageComparator = Comparator.comparingInt(Student::getAge);

        // Sort the list using the Comparator
        Collections.sort(students, ageComparator);

        // Print the sorted list
        System.out.println("Students sorted by age:");
        for (Student student : students) {
            System.out.println("Name: " + student.getName() + ", Age: " + student.getAge());
        }
    }
}

Output:

Students sorted by age:
Name: Ashish, Age: 19
Name: Sachin, Age: 20
Name: Virat, Age: 22
Name: Yuvraj, Age: 24

FAQs

How is a Comparator different from Comparable?

Comparable is an interface that defines the natural ordering of objects. It’s implemented by the class of the objects you want to compare. In contrast, a Comparator is a separate class that defines an external way of comparing objects.

Can I use multiple criteria for sorting using Comparators?

Yes, you can create complex sorting strategies by chaining multiple Comparators using the thenComparing() method provided by the Comparator interface.

Is it possible to sort objects in a case-insensitive manner?

Absolutely. You can create a case-insensitive Comparator using the String.CASE_INSENSITIVE_ORDER comparator or by converting the strings to lowercase before comparison.

What happens if I compare two objects as equal in a Comparator?

If the compare() method returns 0, it indicates that the two objects are considered equal for the sorting order.

Are there any performance considerations when using Comparators?

Comparators add a slight overhead due to the additional method calls during sorting. However, the impact on performance is usually negligible.

Can I use Comparators with arrays?

Yes, you can use the Arrays.sort() method to sort arrays using Comparators. Just provide the Comparator as an additional argument. You can see the example on another post How to use Comparator with Array in Java?

Related Posts:

  • How to Write User Stories and Acceptance Criteria: A…
  • Pagination and Sorting in Spring Boot
  • Expressions in Java
  • Packages in Java: A Guide to Modular, Maintainable Code
  • Collections Class in Java
  • Control Statements in Java
Tags:collection-frameworkjava
Was this article helpful?
โ† Previous ArticleDefine Multipart File Upload Size Limit in Spring Boot Application
Next Article โ†’(Solved) Java 8 date/time types are not supported by default

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap