Switch to Bing in English
About 51,300 results
Open links in new tab
  1. In Java, the Comparator interface is used to define custom sorting logic for objects in a list. It allows you to sort objects based on specific attributes without modifying their class.

    Example: Sorting by Custom Attribute

    Below is an example of sorting a list of objects using a Comparator.

    import java.util.*;

    class Product {
    int id;
    String name;
    double price;

    Product(int id, String name, double price) {
    this.id = id;
    this.name = name;
    this.price = price;
    }

    @Override
    public String toString() {
    return id + " " + name + " $" + price;
    }
    }

    class PriceComparator implements Comparator<Product> {
    @Override
    public int compare(Product p1, Product p2) {
    return Double.compare(p1.price, p2.price); // Ascending order by price
    }
    }

    public class Main {
    public static void main(String[] args) {
    List<Product> products = new ArrayList<>();
    products.add(new Product(1, "Laptop", 800.00));
    products.add(new Product(2, "Phone", 500.00));
    products.add(new Product(3, "Tablet", 300.00));

    System.out.println("Before Sorting:");
    products.forEach(System.out::println);

    Collections.sort(products, new PriceComparator());

    System.out.println("\nAfter Sorting by Price:");
    products.forEach(System.out::println);
    }
    }
    Copied!
    Feedback
  2. Java Advanced Sorting (Comparator and Comparable)

    The Comparator and Comparable interfaces allow you to specify what rule is used to sort objects. Being able to specify a sorting rule also allows you to change how strings and numbers are sorted.

  3. Mastering Java Sorting with Comparators — javaspring.net

    Nov 12, 2025 · The Comparator interface in Java is a powerful tool for customizing the sorting behavior of objects. It allows you to define complex sorting criteria, sort in reverse order, and sort by multiple …

  4. Java List Sorting: Comparable and Comparator - CodeLucky

    Sep 1, 2024 · Learn to sort Java Lists using Comparable and Comparator. Our guide simplifies these essential techniques for effective data management in Java programming.

  5. Java List Sorting: Comparable and Comparator Examples

    Aug 24, 2023 · Learn to sort a List of Objects by a field value using either Comparable or Comparator interface for natural ordering and custom ordering.

  6. Java’s Comparator.comparing() Method Explained - Medium

    Oct 2, 2024 · Learn how to use Java's Comparator.comparing () method for custom sorting of collections by fields like name, date, and price. Includes examples and tips.

  7. How to Sort ArrayList using Comparator? - GeeksforGeeks

    Jul 23, 2025 · Using Comparator we can sort ArrayList on the basis of multiple variables. We can simply implement Comparator without affecting the original User-defined class. To sort an ArrayList using …

  8. How to use Java Comparator for sorting - LabEx

    The tutorial demonstrates how to write custom comparison logic, chain multiple comparators, and apply sorting techniques across various data structures, ultimately enhancing code flexibility and …

  9. Mastering Java Sort Array with Comparator — javaspring.net

    Nov 12, 2025 · In this blog, we will explore the fundamental concepts of using a `Comparator` to sort arrays in Java, along with usage methods, common practices, and best practices.

  10. Sort a list of objects using Comparator in Java - Techie Delight

    Nov 11, 2025 · This post will discuss how to sort a list of objects using Comparator in Java. A Comparator is a comparison function, which provides an ordering for collections of objects that don’t …

By using this site you agree to the use of cookies for analytics, personalized content, and ads.Learn more about third party cookies|Microsoft Privacy Policy