But i couldn't able to sort the date in descending order.
Two easy options:
- You could just reverse your comparison yourself, using
secondDate.compareTo(firstDate). (I assume that in your real code you're actually returningretVal; it's ignored in your posted code.) - Call
Collections.reverseOrder(Comparator)to create a comparator with the reverse order of the original one.
JavaTechOnline
javatechonline.com › home › java 8 comparator comparing date descending
Java 8 Comparator Comparing Date Descending Archives - JavaTechOnline
Home > Posts tagged "java 8 comparator comparing date descending" java · Core Java · Java 8 by devs5003 - January 24, 2024July 16, 20240 · Sorting a List by Date sometimes becomes a bit tricky. One of the tricky part is how to handle date format and then perform the sorting.
Top answer 1 of 4
20
But i couldn't able to sort the date in descending order.
Two easy options:
- You could just reverse your comparison yourself, using
secondDate.compareTo(firstDate). (I assume that in your real code you're actually returningretVal; it's ignored in your posted code.) - Call
Collections.reverseOrder(Comparator)to create a comparator with the reverse order of the original one.
2 of 4
4
Instead of comparing firstDate against secondDate, compare secondDate against firstDate:
retVal = secondDate.compareTo(firstDate);
And don't forget to return retVal instead of 0 ;)
Benchresources
benchresources.net › home › java › java 8 – how to sort list by java.util.date in 4 ways ?
Java 8 - How to Sort List by java.util.Date in 4 ways ? - BenchResources.Net
February 2, 2023 - We will create separate class implementing java.util.Comparator interface and overriding compare() method for ascending-order sorting · For descending-order sorting, we will reverse the Comparator using reversed() method · Finally, we will print product list to console in ascending and descending ...
JavaTechOnline
javatechonline.com › home › comparator.comparing date descending
Comparator.comparing Date Descending Archives - JavaTechOnline
One of the tricky part is how to handle date format and then perform the sorting. In this article 'How to Sort List by Date in Java 8 ?', we will discuss sorting of three types of Dates. These are java.util.Date, java.time.LocalDate and java.time.LocalDateTime.
Baeldung
baeldung.com › home › java › java dates › sorting objects in a list by date
Sorting Objects in a List by Date | Baeldung
April 3, 2025 - This will reverse the comparison and thus return the result in descending order: @Test public void givenEmpList_SortEmpList_thenCheckSortedListDescV1() { Collections.sort(employees, new Comparator<Employee>() { public int compare(Employee emp1, Employee emp2) { return emp2.getJoiningDate().compareTo(emp1.getJoiningDate()); } }); assertEquals(employees, employeesSortedByDateDesc); } We can also convert the above method to more concise forms using the Java 8 Lambda Expressions.
Medium
medium.com › @AlexanderObregon › javas-comparator-comparing-method-explained-342361288af6
Java’s Comparator.comparing() Method Explained | Medium
October 2, 2024 - The Comparator.comparing() method is a highly flexible tool for sorting collections in Java. It simplifies custom sorting based on object properties, making code more readable and maintainable. By understanding how to use comparing() for sorting by various fields like name, date, or price, and considering performance factors such as key extraction and chaining comparators, developers can write efficient and flexible sorting logic.
Sebastian Daschner
blog.sebastian-daschner.com › entries › java_8_comparator
Java 8 Comparator - Sebastian Daschner
Since Java 8, you can also define new Comparators Builder-pattern like: Comparator .comparing(Task::isFinished) .thenComparing(Comparator .comparing(Task::getPriority, Comparator.nullsFirst(Integer::compareTo)).reversed()) .thenComparing(Comparator .comparing(Task::getUpdated, Comparator.nullsFirst(Date::compareTo)).reversed()) .thenComparing(Task::getName); The several order definitions are chained and contain lambda expressions (or just method references as you can see).
Coderanch
coderanch.com › t › 401051 › java › Date-Ascending-order-Comparator
Date Ascending order Comparator (Beginning Java forum at Coderanch)
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... I have a comparator that sorts a collection of objects by date in descending order, but I'm having problem creating a comparator that sorts by date in ascending order.
Top answer 1 of 6
291
You can use Comparator.reverseOrder() to have a comparator giving the reverse of the natural ordering.
If you want to reverse the ordering of an existing comparator, you can use Comparator.reversed().
Sample code:
Stream.of(1, 4, 2, 5)
.sorted(Comparator.reverseOrder());
// stream is now [5, 4, 2, 1]
Stream.of("foo", "test", "a")
.sorted(Comparator.comparingInt(String::length).reversed());
// stream is now [test, foo, a], sorted by descending length
2 of 6
93
You can also use Comparator.comparing(Function, Comparator)
It is convenient to chain comparators when necessary, e.g.:
Comparator<SomeEntity> ENTITY_COMPARATOR =
Comparator.comparing(SomeEntity::getProperty1, Comparator.reverseOrder())
.thenComparingInt(SomeEntity::getProperty2)
.thenComparing(SomeEntity::getProperty3, Comparator.reverseOrder());
CalliCoder
callicoder.com › how-to-compare-date-time-java
How to compare Date and Time in Java | CalliCoder
February 18, 2022 - import java.time.LocalDateTime; public class CompareLocalDateTimeExample { public static void main(String[] args) { LocalDateTime date1 = LocalDateTime.now(); LocalDateTime date2 = LocalDateTime.of(2020, 1, 31, 10, 15, 45); // isAfter() method if(date1.isAfter(date2)) { System.out.println(date1 + " is after " + date2); } // isBefore() method if(date1.isBefore(date2)) { System.out.println(date1 + " is before " + date2); } // isEqual() method if(date1.isEqual(date2)) { System.out.println(date1 + " is equal to " + date2); } // compareTo() method int diff = date1.compareTo(date2); if(diff > 0) { System.out.println(date1 + " is greater than " + date2); } else if (diff < 0) { System.out.println(date1 + " is less than " + date2); } else { System.out.println(date1 + " is equal to " + date2); } } }
Stack Overflow
stackoverflow.com › questions › 21482078 › compare-dates-using-comparator-in-java
android - Compare dates using comparator in Java - Stack Overflow
A simple TreeSet (assuming you have distinct dates) using a Comparator comparing the dateTime.getMillis() of each date for instance would do the trick. And better, why not use directly this "out-of-the-box" Comparator ;) Up to you to implement the exact logic you want. (ascendant, descendant, whatever)
Medium
medium.com › @AlexanderObregon › javas-comparator-reverseorder-method-explained-9f9b8bebd87b
Java’s Comparator.reverseOrder() Method Explained | Medium
January 26, 2025 - By combining the natural ordering logic of the Comparable interface with Comparator.reverseOrder(), you can sort objects in descending order based on a field, such as scores or dates.
Blogger
javarevisited.blogspot.com › 2021 › 09 › comparator-comparing-thenComparing-example-java-.html
Java 8 Comparator comparing() and thenComparing() Example - Tutorial
I like books, so I'll create a Book object with some fields to demonstrate how you can sort a list of books using Java 8 features like lambda expressions and method reference. ... public class Book implements Comparable < Book > { private String title; private String author; private int price; public Book(String title, String author, int price) { this.title = title; this.author = author; this.price = price; } public String getTitle() { return title; } public String getAuthor() { return author; } public int getPrice() { return price; } public void setTitle(String title) { this.title = title; }