Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Comparator.html
Comparator (Java Platform SE 8 )
October 20, 2025 - The returned comparator is serializable if the specified comparator is also serializable. ... For example, to sort a collection of String based on the length and then case-insensitive natural ordering, the comparator can be composed using following code,
Videos
06:48
Java 8 Tutorial 05 :- How to sort a List in Java 8 using Comparator ...
16:00
Java 8 Stream #3 - Sorting with Comparator Example - YouTube
10:43
Java 8 Comparator Example using Lambda - YouTube
07:31
Java8 Comparator Basics - YouTube
Custom Comparator Lambda Expression | Comparator Java 8 ...
Java 8 - Tutorial - 13. Application of Lambda Expression Comparator-4 ...
Javabrahman
javabrahman.com › java-8 › the-complete-java-8-comparator-tutorial-with-examples
The Complete Java 8 Comparator Tutorial with examples
Java 8's Comparator as an assignment target for LambdaExpressions Given the fact that its a functional interface, an instance of a Comparator can now be created in Java 8 with a lambda expression specifying its comparison logic. Take a look at the code snippet below - ... package com.javabrahman.java8.comparator; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import com.javabrahman.java8.Employee; public class ComparatorsInJava8 { static List<Employee> employeeList = Arrays.asList(new Employee("Tom Jones", 45), new Employee("Harry Maj
Mkyong
mkyong.com › home › java8 › java 8 lambda : comparator example
Java 8 Lambda : Comparator example - Mkyong.com
August 5, 2015 - Actually, we can do it even simplier. Instead of writing for example · Comparator salaryComparator = (o1, o2)->o1.getSalary().compareTo(o2.getSalary()); listDevs.sort(salaryComparator.reversed());
Readthedocs
java8tips.readthedocs.io › en › stable › comparator.html
7. Comparator — Java 8 tips 1.0 documentation
It is very much possible that two elements will be equal according to the given comparator. In such cases the other comprator decides the sorting order. Below code snippet shows example of sorting employee objects based on employee’s salary and then uses name if two salaries are equal.
Baeldung
baeldung.com › home › java › core java › comparator and comparable in java
Comparator and Comparable in Java | Baeldung
March 26, 2025 - Java 8 provides new ways of defining Comparators by using lambda expressions, and the comparing() static factory method. Let’s see a quick example of how to use a lambda expression to create a Comparator:
GeeksforGeeks
geeksforgeeks.org › java › java-comparator-interface
Java Comparator Interface - GeeksforGeeks
Collections.sort() uses this comparator to order students. Final list shows sorted order by both name and age fields. Java 8 introduced a more simple way to write comparators using lambda expressions.
Published April 20, 2016
HowToDoInJava
howtodoinjava.com › home › java 8 › java comparator with lambda
Java Comparator with Lambda (with Examples) - HowToDoInJava
February 6, 2023 - Learn to create a Comparator instance with lambda expressions, method references and chaining multiple comparators for complex comparisons. Lokesh Gupta · February 6, 2023 · Java 8 · Java 8, Java Compare, Lambda Expression · The Comparator interface is used to sort a collection of objects that can be compared.
Blogger
javarevisited.blogspot.com › 2023 › 04 › 7-examples-of-comparator-and-comparable.html
7 Examples of Comparator and Comparable in Java 8
Hello guys, you may know that Java 8 introduced a number of features to simplify the code and make it more readable. One such feature is enhancements of Comparator and Comparable interfaces. The Comparator interface provides a way to compare two objects and sort them based on specific criteria. The Comparable interface is used to compare the objects of the same class and sort them based on their natural order. In this article, we'll explore 7 different examples of Comparator and Comparable in Java 8.
Java67
java67.com › 2019 › 06 › top-5-sorting-examples-of-comparator-and-comparable-in-java.html
6 Advanced Comparator and Comparable Examples in Java 8 | Java67
If you are amazed to see the previous example of Comparator, you will be even more surprised to see this example, which demonstrates another magic of Java 8 which allows you to chain methods to perform sophisticated comparison logic. One of the common requirements in the real world is to compare objects by multiple fields like the first compare by the author and if the author is same the as compare by title or price.
Readthedocs
java-8-tips.readthedocs.io › en › stable › comparator.html
7. Comparator — Java 8 tips 1.0 documentation
It is very much possible that two elements will be equal according to the given comparator. In such cases the other comprator decides the sorting order. Below code snippet shows example of sorting employee objects based on employee’s salary and then uses name if two salaries are equal.
Javatpoint
javatpoint.com › Comparator-interface-in-collection-framework
Java Comparator - javatpoint
Java Comparator interface is used to order the user-defined class objects, compare() method, collection class, java comporator example, Example of Comparator interface in collection framework.
Medium
medium.com › @AlexanderObregon › javas-comparator-comparing-method-explained-342361288af6
Java’s Comparator.comparing() Method Explained | Medium
October 2, 2024 - We'll go through a detailed example using a class Product, demonstrating how sorting can be done on various fields, as well as combining sorting conditions to meet specific requirements. To illustrate the power of Comparator.comparing(), we’ll work with a Product class that has three main attributes: a product name, price, and a manufacture date. Here’s how the class is structured: import java.time.LocalDate; public class Product { private String name; private double price; private LocalDate manufactureDate; public Product(String name, double price, LocalDate manufactureDate) { this.name =
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › class-use › Comparator.html
Uses of Interface java.util.Comparator (Java Platform SE 8 )
October 20, 2025 - 8 ... Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Praveer09
praveer09.github.io › technology › 2016 › 06 › 20 › writing-comparators-the-java8-way
Writing Comparators - The Java 8 Way
Now let’s try to write a Comparator that would sort a collection of Person in the ascending order of a Person’s name. The question that would arise in your mind is that what should be sorting criteria for the null values of name? Let’s keep the Person objects, having null names, towards the end of the sorted collection. import static java.util.Comparator.nullsLast; ...
Top answer 1 of 7
91
Comparator#compareTo returns an int; while getTime is obviously long.
It would be nicer written like this:
.sort(Comparator.comparingLong(Message::getTime))
2 of 7
38
Lambda
The lambda can be seen as the shorthand of somewhat cumbersome anonymous class:
Java8 version:
Collections.sort(list, (o1, o2) -> o1.getTime() - o2.getTime());
Pre-Java8 version:
Collections.sort(list, new Comparator<Message>() {
@Override
public int compare(Message o1, Message o2) {
return o1.getTime() - o2.getTime();
}
});
So, every time you are confused how to write a right lambda, you may try to write a pre-lambda version, and see how it is wrong.
Application
In your specific problem, you can see the compare returns int, where your getTime returns long, which is the source of error.
You may use either method as other answer method, like:
Long.compare(o1.getTime(),o2.getTime())
Notice
- You should avoid using
-inComparator, which may causes overflow, in some cases, and crash your program.
Dev.java
dev.java › learn › lambdas › writing-comparators
Writing and Combining Comparators - Dev.java
Here is an example, where you want ... for the naturalOrder() method to further improve readability): Comparator<String> byLengthThenAlphabetically = Comparator.comparing(String::length) .thenComparing(naturalOrder()); List<String> ...