How to Sort Java List Objects by Date property with Examples

java-list-sorting-by-date-property-java-8


In the tutorial, We discuss how to Sort Java List Objects by Date property with difference Date types: java.util.Date(using SimpleDateFormat), LocalDate, LocalDateTime. Java provides 2 main approaches for sorting Java List with Comparator:

  • java.util.Collections.sort(List list, Comparator c): sorting the specified list according to the order providing by the specified comparator.
  • java.util.Collection.stream().sorted(Comparator comparator): returning a stream consisting of the elements of this stream, sorted according to the provided Comparator.

Let’s do more details with Java syntax examples (Java 7 and Java 8) by descending and ascending sorting order.

Sorting with Collections.sort()

For sorting a Java List with Objects, we use Collections.sort() API:

– Sorts the specified list according to the order induced by the specified comparator.
– According to Oracle: “This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. If the input array is nearly sorted, the implementation requires approximately n comparisons. Temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays.”

Exception:
ClassCastException – if the list contains elements that are not mutually comparable using the specified comparator.
UnsupportedOperationException – if the specified list’s list-iterator does not support the set operation.
IllegalArgumentException – (optional) if the comparator is found to violate the Comparator contract

Sorting Java Objects Examples by java.util.Date property

We create a Java Class Object that has a birthday property with java.util.Date type as below:

– Create a List of Customer as below:

– Let’s do sorting!

Define a Sorting Comparator

Create a Comparator with Customer class, in compare function, we get time of Date to compare them:

– Full Example:

– Output:

* We also can re-implemetation by using Anonymous Comparator Inner Class:

Implement with Java 8 Lamda Expression

With the powerful of Lamda Expression, We can implement the Sorting Java Object List with Java 8 by Date Property more simply:

Note: We recommend to use the implementation with Java 8 Lamda Expression for more simple, readability.

Sorting Java Objects Examples by java.time.LocalDate property

We create a Java Class Object that has a birthday property with java.time.LocalDate type as below:

– Create a List of Customer as below:

Define a Sorting Comparator

Create a Comparator with Customer class, in compare function, we get time of LocalDate to compare them:

– Full Example:

-> Output:

– We also can re-implemetation by using Anonymous Comparator Inner Class:

Implement with Java 8 Lamda Expression

We implement a simple solution with Java 8 Lamda Express:

Sorting Java Objects Examples by java.time.LocalDateTime property

With LocalDateTime property type, We do the sorting as the same way with LocalDate.

– Full Example with Java 8 Lamda Expression:

– Output:

Descending Sorting with Collections.sort()

For descending sorting with Java list objects, We have 2 approaches:
– Modify Comparator for descending comparing order
– Or use reversed() function of comparator – Recommended

Let’s do examples with LocalDateTime:

Descending Sorting by Modify Comparator

We use isAfter() function to re-define the Comparator with descending comparing, then do sorting:

– Full example:

-> Output:

Use reversed() function of Comparator

We do NOT modify the comparator But using reversed() function for descending sorting:

– Full example:

-> Output:

Sorting Java Object List by Date property with Collection.stream()

Java 8 provides Stream with new APIs for sorting List:

-> It returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.

With the powerful of stream, We can use it to create a new sorted List:

-> Full Example:

-> Output:

Conclusion

In this post, You learned how to sort with Java Object List by date proprety(java.util.Date, LocalDate, LocalDateTime):
– Sorting Java Object List by Date proprety in Ascending & Descending order.
– Use Java Comparator & Java 8 with Lamda Expression syntax to implement examples.
– Implementing with collection sort API Collections.sort() & new stream sort API: Collection.stream().sorted()

Hope it helpful for you! Thank you. See you next time.

By grokonez | March 21, 2019.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*