How to Sort Java List Objects by Date property with Examples
Tags: java, sorting, date, example
Published: 2021-05-10 09:23:27

https://grokonez.com/java/how-to-sort-java-list-objects-by-date-property-with-examples

How to Sort Java List Objects by Date property with Examples

In the tutorial, We discuss how to Sort Java List Objects by Date property with difference Date types: <code>java.util.Date</code>(using <code>SimpleDateFormat</code>), <code>LocalDate</code>, <code>LocalDateTime</code>. Java provides 2 main approaches for sorting Java List with Comparator:
<ul>
<li>Using <code>java.util.Collections.sort(List<Customer> list, Comparator<? super Customer> c)</code>: sorting the specified list according to the order providing by the specified comparator. 
</li>
<li>Using Java 8 Stream API to sort <code>java.util.Collection.stream().sorted(Comparator<? super T> comparator)</code>: returning a stream consisting of the elements of this stream, sorted according to the provided Comparator.
</li>
</ul>

Let's do more details with Java syntax examples (Java 7 and Java 8 Stream + Lamda Expression) by descending and ascending sorting order.

<!--more-->

<h2 id="sorting-with-collections-sort">Overview Sorting with Collections.sort()</h2>
For sorting a Java List with Objects, we use <code>Collections.sort()</code> API:

<pre><code class="language-java">public static &lt;T&gt; void sort(List&lt;T&gt; list, Comparator&lt;? super T&gt; c) {
	list.sort(c);
}</code></pre>

- 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:
- <code>ClassCastException</code> - if the list contains elements that are not mutually comparable using the specified comparator.
- <code>UnsupportedOperationException</code> - if the specified list's list-iterator does not support the set operation.
- <code>IllegalArgumentException</code> - (optional) if the comparator is found to violate the Comparator contract
<h2 id="sorting-java-object-examples-by-date-property">Using Collections.sort() - Sorting Java Objects Examples by java.util.Date property</h2>
We create a Java Class Object that has a <code>birthday</code> property with <code>java.util.Date</code> type as below:

<pre><code class="language-java">
import java.text.SimpleDateFormat;
import java.util.Date;

class Customer {
	private int id;
	private String name;
	private Date birthday;
	
	public Customer(int id, String name, Date birthday){
		this.id = id;
		this.name = name;
		this.birthday = birthday;
	}
	
	public int getId() {
		return id;
	}
	
	public Date getBirthday() {
		return this.birthday;
	}
	
	public String toString() {
		return String.format("id = %d, name = %s, birthday = %s", this.id, this.name, this.birthday.toString());
	}
}
</code></pre>

- Create a List of Customer as below:

More at:

https://grokonez.com/java/how-to-sort-java-list-objects-by-date-property-with-examples

How to Sort Java List Objects by Date property with Examples