Open links in new tab
  1. The import java.util.Comparator; statement in Java allows you to use the Comparator interface, which defines custom sorting logic for objects without modifying their class definition. It is ideal when you need multiple sorting strategies or want to keep sorting logic separate from the model class.

    Example – Sorting by Roll Number

    import java.util.*;

    class Student {
    int rollno;
    String name;
    Student(int rollno, String name) {
    this.rollno = rollno;
    this.name = name;
    }
    @Override
    public String toString() {
    return rollno + ": " + name;
    }
    }

    class SortByRoll implements Comparator<Student> {
    public int compare(Student a, Student b) {
    return a.rollno - b.rollno; // ascending order
    }
    }

    public class Main {
    public static void main(String[] args) {
    List<Student> students = new ArrayList<>();
    students.add(new Student(111, "Mayank"));
    students.add(new Student(131, "Anshul"));
    students.add(new Student(121, "Solanki"));
    students.add(new Student(101, "Aggarwal"));

    Collections.sort(students, new SortByRoll());
    System.out.println("Sorted by Roll Number:");
    for (Student s : students) {
    System.out.println(s);
    }
    }
    }
    Copied!
  1. Java Advanced Sorting (Comparator and Comparable) - W3Schools

    An object that implements the Comparator interface is called a comparator. The Comparator interface allows you to create a class with a compare() method that compares two objects to decide which one …

  2. Java Comparator Interface - GeeksforGeeks

    Feb 2, 2026 · The Comparator interface in Java is used to define custom sorting logic for objects. It belongs to java.util package allows sorting of objects of user …

  3. Guide to Java Comparator.comparing () - Baeldung

    Java 8 introduced several enhancements to the Comparatorinterface, including a handful of static functions that are of great utility when coming up with a sort order for collections. The Comparator interface can also effectively leverage Java 8 lambdas. A detailed explanation of lambdas and Comparator can be found here, and a chronicle on t…
    See more on baeldung.com
    • Published: Mar 30, 2017
    • Comparator (Java Platform SE 8 ) - Oracle Help Center

      Unlike Comparable, a comparator may optionally permit comparison of null arguments, while maintaining the requirements for an equivalence relation. This interface is a member of the Java …

    • Java - How to Use Comparator? - Online Tutorials Library

      Java Comparator Interface In Java, the Comparator interface is a part of java.util package and it defines the order of the objects of user-defined classes.

    • Java Comparator Tutorial: How to Sort and Fix ClassCastException …

      Jan 16, 2026 · This tutorial will demystify `Comparator`, show you how to use it effectively, and teach you to avoid/common fix `ClassCastException` when moving from `Comparable`.

    • Understanding Java Comparator and Comparable: A Comprehensive …

      In Java, sorting collections of objects can be achieved through two powerful interfaces: Comparator and Comparable. This tutorial dives deep into both interfaces, illustrating their use, differences, and how …

    • Comparator Methods and Examples - GeeksforGeeks

      Jan 29, 2026 · The Comparator interface in Java is used to define custom sorting logic for objects. It allows sorting collections based on different attributes without modifying the original class.

    • Java Comparator Interface Tutorial

      In this tutorial, we will explore the Comparator interface in Java, which allows for custom sorting logic. We will demonstrate how to use Comparator to sort a list of custom objects based on various …

    • Comparator and Comparable in Java - Baeldung

      Mar 26, 2025 · Comparisons in Java are quite easy, until they’re not. When working with custom types, or trying to compare objects that aren’t directly comparable, …