A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-comparator-interface/ below:

Java Comparator Interface - GeeksforGeeks

Java Comparator Interface

Last Updated : 31 Jul, 2025

The Comparator interface in Java is used to sort the objects of user-defined classes. The Comparator interface is present in java.util package. This interface allows us to define custom comparison logic outside of the class for which instances we want to sort. The comparator interface is useful when,

A comparator object is capable of comparing two objects of the same class. The following function compares obj1 with obj2.

Syntax

public int compare(Object obj1, Object obj2):

When to Use Comparator? Methods to Implement Comparator Interface

How does the sort() Method of the Collections Class Work? 

The sort() method of the Collections class is used to sort the elements of a List by the given comparator.  

public void sort(List list, ComparatorClass c)

To sort a given List, ComparatorClass must implement a Comparator interface.

Internally the sort() method does call Compare method of the classes it is sorting. To compare two elements, it asks “Which is greater?” Compare method returns -1, 0 or 1 to say if it is less than, equal or greater to the other. It uses this result to then determine if they should be swapped for their sort.

Sort Collections by One Field

Example: Sorting By Roll Number

Java
import java.util.*;

// Define the Student class
class Student {
    int rollno;
    String name;

  	// Constructor
    Student(int rollno, String name) {
        this.rollno = rollno;
        this.name = name;
    }

  	// Method to print Student details in main()
    @Override
    public String toString() {
        return rollno + ": " + name;
    }
}

// Helper class implementing Comparator interface
class SortbyRoll implements Comparator<Student> 
{    
  	// Compare by roll number in ascending order
    public int compare(Student a, Student b) {
        return a.rollno - b.rollno;
    }
}

// Driver Class
public class Geeks
{
    public static void main(String[] args) 
    {
      	// List of Students
        List<Student> students = new ArrayList<>();
        
      	// Add Elements in List
      	students.add(new Student(111, "Mayank"));
        students.add(new Student(131, "Anshul"));
        students.add(new Student(121, "Solanki"));
        students.add(new Student(101, "Aggarwal"));

        // Sort students by roll number using SortbyRoll comparator
        Collections.sort(students, new SortbyRoll());

      	System.out.println("Sorted by Roll Number ");

        // Iterating over entries to print them
        for (int i = 0; i < students.size(); i++)
            System.out.println(students.get(i));
      	
    }
}

Output
Sorted by Roll Number 
101: Aggarwal
111: Mayank
121: Solanki
131: Anshul

By changing the return value inside the compare method, you can sort in any order that you wish to. For example: For descending order just change the positions of "a" and "b" in the above compare method.

Note: We can use lambda expression in place of helper function by following the statement as mentioned below:

students.sort((p1, p2) -> Integer.compare(p1.age, p2.age));

Sort Collection by More than One Field

In the previous example, we have discussed how to sort the list of objects on the basis of a single field using the Comparable and Comparator interface But, what if we have a requirement to sort ArrayList objects in accordance with more than one field like firstly, sort according to the student name and secondly, sort according to student age.

Example: Sorting By Multiple Fields (Name, then Age)

Java
import java.util.*;

// Define the Student class
class Student 
{
    String name;
    Integer age;

  	// Constructor
    Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

  	// Method to print student details in main()
    @Override
    public String toString() {
        return name + " : " + age;
    }
}

// Comparator in a Helper Class
class CustomerSortingComparator 
  		implements Comparator<Student> 
{
    
  	// Compare first by name, then by age
    public int compare(Student customer1, Student customer2) {
        
      	// Compare by name first
        int NameCompare = customer1.getName().compareTo(
          						customer2.getName());
        
        // If names are the same, compare by age
        int AgeCompare = customer1.getAge().compareTo(
          						customer2.getAge());
        
        // Return the result: first by name, second by age
        return (NameCompare == 0) ? AgeCompare : NameCompare;
    }
}

public class ComparatorHelperClassExample 
{
    public static void main(String[] args) 
    {
        List<Student> students = new ArrayList<>();
      
        students.add(new Student("Ajay", 27));
        students.add(new Student("Sneha", 23));
        students.add(new Student("Simran", 37));
        students.add(new Student("Ankit", 22));
      	students.add(new Student("Anshul", 29));
      	students.add(new Student("Sneha", 22));
      
      	// Original List
        System.out.println("Original List ");

        // Iterating List
        for (Student it : students) {
            System.out.println(it);
        }
      
      	System.out.println();

        // Sort students by name, then by age using the CustomerSortingComparator
        Collections.sort(students, new CustomerSortingComparator());

        // Display message only
        System.out.println("After Sorting ");

        // Iterating using enhanced for-loop after Sorting ArrayList
        for (Student it : students) {
            System.out.println(it);
        }
    }
}

Output
Original List 
Ajay : 27
Sneha : 23
Simran : 37
Ankit : 22
Anshul : 29
Sneha : 22

After Sorting 
Ajay : 27
Ankit : 22
Anshul : 29
Simran : 37
Sneha : 22
Sneha : 23
Alternative Method: Using Comparator with Lambda

Java 8 introduced a more simple way to write comparators using lambda expressions. We can use the method mentioned below for achieving same result:

students.sort(Comparator.comparing(Student::getName).thenComparing(Student::getAge));

Example:

Java
import java.util.*;

// Define the Student class
class Student {
    String name;
    Integer age;

    // Constructor
    Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    // Method to print student details
    @Override
    public String toString() {
        return name + " : " + age;
    }
}

public class ComparatorHelperClassExample {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();

        students.add(new Student("Ajay", 27));
        students.add(new Student("Sneha", 23));
        students.add(new Student("Simran", 37));
        students.add(new Student("Ankit", 22));
        students.add(new Student("Anshul", 29));
        students.add(new Student("Sneha", 22));

        // Original List
        System.out.println("Original List:");

        // Iterating List
        for (Student it : students) {
            System.out.println(it);
        }

        System.out.println();

        // Sort students by name, then by age
        students.sort(Comparator.comparing(Student::getName).thenComparing(Student::getAge));

        // Display message after sorting
        System.out.println("After Sorting:");

        // Iterating using enhanced for-loop after sorting ArrayList
        for (Student it : students) {
            System.out.println(it);
        }
    }
}

Output
Original List:
Ajay : 27
Sneha : 23
Simran : 37
Ankit : 22
Anshul : 29
Sneha : 22

After Sorting:
Ajay : 27
Ankit : 22
Anshul : 29
Simran : 37
Sneha : 22
Sneha : 23
Comparator vs Comparable Comparator Comparable Sorting logic is defined externally. Sorting logic is defined within the class (Internally) Supports multiple sorting orders. Does not support multiple sorting orders. Uses the compare() method Uses the compareTo() method. It is a functional interface. It is also a functional interface (since Java 8) More flexible and reusable Simple and tightly coupled

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4