- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
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;}@Overridepublic String toString() {return id + " " + name + " $" + price;}}class PriceComparator implements Comparator<Product> {@Overridepublic 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!✕Copy Sort an Array in Java using Comparator - GeeksforGeeks
Jul 23, 2025 · A Comparator is an object that can be used to compare two objects and determine their order. We can use a Comparator to sort a list of objects in any order we can choose, not just in …
See results only from geeksforgeeks.orgSign In
A Comparator is an object that can be …
How to Sort ArrayList using …
Using Comparator we can sort ArrayList …
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.
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 …
- People also ask
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.
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.
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.
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 …
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 …
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.
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 …