In this article, we will discuss and learn how to sort List by java.util.Date in ascending as well as descending order in different ways
Sort List by java.util.Date in 4 ways:
- Using java.util.Comparator
- Using Lambda expression
- Using Method References
- Using Stream API
We have Product class with 4 member variables, its setters & getters, 4-arg parameterized constructor and overriding toString() method for pretty print. We will sort List of Products on the basis of java.util.Date in ascending and descending order in 4 different ways as mentioned above.
Product.java
package in.bench.resources.date.sorting;
import java.util.Date;
public class Product {
// member variables
private int prodId;
private String prodName;
private Date prodCreatedDate;
private double prodRate;
// getters and setters
// 4-arg parameterized constructor
// toString() method
@Override
public String toString() {
return "Product [prodId=" + prodId
+ ", prodCreatedDate=" + prodCreatedDate
+ ", prodRate=" + prodRate
+ ", prodName=" + prodName
+ "]";
}
}
1. Sort List by Date using java.util.Comparator
- There is a list of Products and we will be sorting product list by java.util.Date in ascending and descending order using java.util.Comparator interface
- For sorting,
- 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 order
ProductSortingComparator.java
package in.bench.resources.date.sorting;
import java.util.Comparator;
public class ProductSortingComparator implements Comparator<Product> {
@Override
public int compare(Product prod1, Product prod2) {
return Long.valueOf(prod1.getProdCreatedDate().getTime())
.compareTo(prod2.getProdCreatedDate().getTime());
}
}
ProductDateSortingUsingComparator.java
package in.bench.resources.date.sorting;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ProductDateSortingUsingComparator {
public static void main(String[] args) throws ParseException {
// 1. products
Product product1 = new Product(1, "Cooker",
new SimpleDateFormat("yyyy-MM-dd").parse("2022-05-31"), 13750.0);
Product product2 = new Product(2, "Sofa-Bed",
new SimpleDateFormat("yyyy-MM-dd").parse("2019-11-02"), 11750.0);
Product product3 = new Product(3, "Fridge",
new SimpleDateFormat("yyyy-MM-dd").parse("2021-03-27"), 12750.0);
Product product4 = new Product(4, "Cupboard",
new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-30"), 11525.0);
Product product5 = new Product(5, "Utensils",
new SimpleDateFormat("yyyy-MM-dd").parse("2022-06-01"), 17050.0);
// 1.1 List of Products
List<Product> products = new ArrayList<Product>();
products.add(product1);
products.add(product2);
products.add(product3);
products.add(product4);
products.add(product5);
// 1.2 print to console
System.out.println("Before sorting :- \n");
for(Product prod : products) {
System.out.println(prod);
}
// 2. Ascending-order sorting
Collections.sort(products, new ProductSortingComparator());
// 2.1 print to console - ascending order
System.out.println("\n\nAscending-order sorting on the basis of Created Date :- \n");
for(Product prod : products) {
System.out.println(prod);
}
// 3. Descending-order sorting
Collections.sort(products, (new ProductSortingComparator()).reversed());
// 3.1 print to console - descending order
System.out.println("\n\nDescending-order sorting on the basis of Created Date :- \n");
for(Product prod : products) {
System.out.println(prod);
}
}
}
Output:
Before sorting :-
Product [prodId=1, prodCreatedDate=Tue May 31 00:00:00 IST 2022, prodRate=13750.0, prodName=Cooker]
Product [prodId=2, prodCreatedDate=Sat Nov 02 00:00:00 IST 2019, prodRate=11750.0, prodName=Sofa-Bed]
Product [prodId=3, prodCreatedDate=Sat Mar 27 00:00:00 IST 2021, prodRate=12750.0, prodName=Fridge]
Product [prodId=4, prodCreatedDate=Thu Apr 30 00:00:00 IST 2020, prodRate=11525.0, prodName=Cupboard]
Product [prodId=5, prodCreatedDate=Wed Jun 01 00:00:00 IST 2022, prodRate=17050.0, prodName=Utensils]
Ascending-order sorting on the basis of Created Date :-
Product [prodId=2, prodCreatedDate=Sat Nov 02 00:00:00 IST 2019, prodRate=11750.0, prodName=Sofa-Bed]
Product [prodId=4, prodCreatedDate=Thu Apr 30 00:00:00 IST 2020, prodRate=11525.0, prodName=Cupboard]
Product [prodId=3, prodCreatedDate=Sat Mar 27 00:00:00 IST 2021, prodRate=12750.0, prodName=Fridge]
Product [prodId=1, prodCreatedDate=Tue May 31 00:00:00 IST 2022, prodRate=13750.0, prodName=Cooker]
Product [prodId=5, prodCreatedDate=Wed Jun 01 00:00:00 IST 2022, prodRate=17050.0, prodName=Utensils]
Descending-order sorting on the basis of Created Date :-
Product [prodId=5, prodCreatedDate=Wed Jun 01 00:00:00 IST 2022, prodRate=17050.0, prodName=Utensils]
Product [prodId=1, prodCreatedDate=Tue May 31 00:00:00 IST 2022, prodRate=13750.0, prodName=Cooker]
Product [prodId=3, prodCreatedDate=Sat Mar 27 00:00:00 IST 2021, prodRate=12750.0, prodName=Fridge]
Product [prodId=4, prodCreatedDate=Thu Apr 30 00:00:00 IST 2020, prodRate=11525.0, prodName=Cupboard]
Product [prodId=2, prodCreatedDate=Sat Nov 02 00:00:00 IST 2019, prodRate=11750.0, prodName=Sofa-Bed]
2. Sort List by Date using Lambda Expression
- There is a list of Products and we will be sorting product list by java.util.Date in ascending and descending order using Lambda Expression
- For sorting, we will write/code lambda expression to sort list of product on the basis of product created date which will return Comparator
- For ascending-order sorting, we will pass actual list and Comparator to Collections.sort() method
- For descending-order sorting, we will reverse the Comparator using reversed() method and pass actual list and reversed Comparator to Collections.sort() method
- Finally, we will print product list to console in ascending and descending order
ProductDateSortingUsingLambdaExpression.java
package in.bench.resources.date.sorting;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ProductDateSortingUsingLambdaExpression {
public static void main(String[] args) throws ParseException {
// 1. products
Product product1 = new Product(1, "Cooker",
new SimpleDateFormat("yyyy-MM-dd").parse("2022-05-31"), 13750.0);
Product product2 = new Product(2, "Sofa-Bed",
new SimpleDateFormat("yyyy-MM-dd").parse("2019-11-02"), 11750.0);
Product product3 = new Product(3, "Fridge",
new SimpleDateFormat("yyyy-MM-dd").parse("2021-03-27"), 12750.0);
Product product4 = new Product(4, "Cupboard",
new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-30"), 11525.0);
Product product5 = new Product(5, "Utensils",
new SimpleDateFormat("yyyy-MM-dd").parse("2022-06-01"), 17050.0);
// 1.1 List
List<Product> products = new ArrayList<Product>();
products.add(product1);
products.add(product2);
products.add(product3);
products.add(product4);
products.add(product5);
// 1.2 print to console
System.out.println("Before sorting :- \n");
products.forEach(prod -> System.out.println(prod));
// 2. get Comparator using Lambda expression
Comparator<Product> comparatorAsc = (prod1, prod2) -> Long.valueOf(
prod1.getProdCreatedDate().getTime())
.compareTo(prod2.getProdCreatedDate().getTime()
);
// 2.1 pass above Comparator and sort in ascending order
Collections.sort(products, comparatorAsc);
// 2.2 print to console
System.out.println("\n\nAscending-order sorting on the basis of Created Date :- \n");
products.forEach(prod -> System.out.println(prod));
// 3. get Comparator using Lambda expression
Comparator<Product> comparatorDesc = (prod1, prod2) -> Long.valueOf(
prod2.getProdCreatedDate().getTime())
.compareTo(prod1.getProdCreatedDate().getTime()
);
// 3.1 pass above Comparator and sort in descending order
Collections.sort(products, comparatorDesc);
// 3.2 print to console - descending order
System.out.println("\n\nDescending-order sorting on the basis of Created Date :- \n");
products.forEach(prod -> System.out.println(prod));
}
}
Output:
Before sorting :-
Product [prodId=1, prodCreatedDate=Tue May 31 00:00:00 IST 2022, prodRate=13750.0, prodName=Cooker]
Product [prodId=2, prodCreatedDate=Sat Nov 02 00:00:00 IST 2019, prodRate=11750.0, prodName=Sofa-Bed]
Product [prodId=3, prodCreatedDate=Sat Mar 27 00:00:00 IST 2021, prodRate=12750.0, prodName=Fridge]
Product [prodId=4, prodCreatedDate=Thu Apr 30 00:00:00 IST 2020, prodRate=11525.0, prodName=Cupboard]
Product [prodId=5, prodCreatedDate=Wed Jun 01 00:00:00 IST 2022, prodRate=17050.0, prodName=Utensils]
Ascending-order sorting on the basis of Created Date :-
Product [prodId=2, prodCreatedDate=Sat Nov 02 00:00:00 IST 2019, prodRate=11750.0, prodName=Sofa-Bed]
Product [prodId=4, prodCreatedDate=Thu Apr 30 00:00:00 IST 2020, prodRate=11525.0, prodName=Cupboard]
Product [prodId=3, prodCreatedDate=Sat Mar 27 00:00:00 IST 2021, prodRate=12750.0, prodName=Fridge]
Product [prodId=1, prodCreatedDate=Tue May 31 00:00:00 IST 2022, prodRate=13750.0, prodName=Cooker]
Product [prodId=5, prodCreatedDate=Wed Jun 01 00:00:00 IST 2022, prodRate=17050.0, prodName=Utensils]
Descending-order sorting on the basis of Created Date :-
Product [prodId=5, prodCreatedDate=Wed Jun 01 00:00:00 IST 2022, prodRate=17050.0, prodName=Utensils]
Product [prodId=1, prodCreatedDate=Tue May 31 00:00:00 IST 2022, prodRate=13750.0, prodName=Cooker]
Product [prodId=3, prodCreatedDate=Sat Mar 27 00:00:00 IST 2021, prodRate=12750.0, prodName=Fridge]
Product [prodId=4, prodCreatedDate=Thu Apr 30 00:00:00 IST 2020, prodRate=11525.0, prodName=Cupboard]
Product [prodId=2, prodCreatedDate=Sat Nov 02 00:00:00 IST 2019, prodRate=11750.0, prodName=Sofa-Bed]
3. Sort List by Date using Method References
- There is a list of Products and we will be sorting product list by java.util.Date in ascending and descending order using Method References
- For sorting, create a Comparator by passing Method References to Comparator.comparing() method
- For ascending-order sorting, pass above created Comparator to List.sort() method
- For descending-order sorting, we will reverse the Comparator using reversed() method and pass reversed Comparator to List.sort() method
- Finally, we will print product list to console in ascending and descending order
ProductDateSortingUsingMethodReferences.java
package in.bench.resources.date.sorting;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class ProductDateSortingUsingMethodReferences {
public static void main(String[] args) throws ParseException {
// 1. products
Product product1 = new Product(1, "Cooker",
new SimpleDateFormat("yyyy-MM-dd").parse("2022-05-31"), 13750.0);
Product product2 = new Product(2, "Sofa-Bed",
new SimpleDateFormat("yyyy-MM-dd").parse("2019-11-02"), 11750.0);
Product product3 = new Product(3, "Fridge",
new SimpleDateFormat("yyyy-MM-dd").parse("2021-03-27"), 12750.0);
Product product4 = new Product(4, "Cupboard",
new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-30"), 11525.0);
Product product5 = new Product(5, "Utensils",
new SimpleDateFormat("yyyy-MM-dd").parse("2022-06-01"), 17050.0);
// 1.1 List
List<Product> products = new ArrayList<Product>();
products.add(product1);
products.add(product2);
products.add(product3);
products.add(product4);
products.add(product5);
// 1.2 print to console
System.out.println("Before sorting :- \n");
products.forEach(System.out::println);
// 2. ascending-order sorting using Method References
products.sort(Comparator.comparing(Product::getProdCreatedDate));
// 2.1 print to console
System.out.println("\n\nAscending-order sorting on the basis of Created Date :- \n");
products.forEach(System.out::println);
// 3. descending-order sorting using Method References
products.sort(Comparator.comparing(Product::getProdCreatedDate).reversed());
// 3.1 print to console - descending order
System.out.println("\n\nDescending-order sorting on the basis of Created Date :- \n");
products.forEach(System.out::println);
}
}
Output:
Before sorting :-
Product [prodId=1, prodCreatedDate=Tue May 31 00:00:00 IST 2022, prodRate=13750.0, prodName=Cooker]
Product [prodId=2, prodCreatedDate=Sat Nov 02 00:00:00 IST 2019, prodRate=11750.0, prodName=Sofa-Bed]
Product [prodId=3, prodCreatedDate=Sat Mar 27 00:00:00 IST 2021, prodRate=12750.0, prodName=Fridge]
Product [prodId=4, prodCreatedDate=Thu Apr 30 00:00:00 IST 2020, prodRate=11525.0, prodName=Cupboard]
Product [prodId=5, prodCreatedDate=Wed Jun 01 00:00:00 IST 2022, prodRate=17050.0, prodName=Utensils]
Ascending-order sorting on the basis of Created Date :-
Product [prodId=2, prodCreatedDate=Sat Nov 02 00:00:00 IST 2019, prodRate=11750.0, prodName=Sofa-Bed]
Product [prodId=4, prodCreatedDate=Thu Apr 30 00:00:00 IST 2020, prodRate=11525.0, prodName=Cupboard]
Product [prodId=3, prodCreatedDate=Sat Mar 27 00:00:00 IST 2021, prodRate=12750.0, prodName=Fridge]
Product [prodId=1, prodCreatedDate=Tue May 31 00:00:00 IST 2022, prodRate=13750.0, prodName=Cooker]
Product [prodId=5, prodCreatedDate=Wed Jun 01 00:00:00 IST 2022, prodRate=17050.0, prodName=Utensils]
Descending-order sorting on the basis of Created Date :-
Product [prodId=5, prodCreatedDate=Wed Jun 01 00:00:00 IST 2022, prodRate=17050.0, prodName=Utensils]
Product [prodId=1, prodCreatedDate=Tue May 31 00:00:00 IST 2022, prodRate=13750.0, prodName=Cooker]
Product [prodId=3, prodCreatedDate=Sat Mar 27 00:00:00 IST 2021, prodRate=12750.0, prodName=Fridge]
Product [prodId=4, prodCreatedDate=Thu Apr 30 00:00:00 IST 2020, prodRate=11525.0, prodName=Cupboard]
Product [prodId=2, prodCreatedDate=Sat Nov 02 00:00:00 IST 2019, prodRate=11750.0, prodName=Sofa-Bed]
4. Sort List by Date using Stream API
- There is a list of Products and we will be sorting product list by java.util.Date in ascending and descending order using Stream API
- For sorting, create a Comparator by passing Method References to Comparator.comparing() method
- For ascending-order sorting, get Stream from the Product list and pass above created Comparator to Stream.sorted() method and collect to a new List using Stream.collect() method
- For descending-order sorting, get Stream from the Product list and pass reversed Comparator to Stream.sorted() method and collect to a new List using Stream.collect() method
- Finally, we will print product list to console in ascending and descending order
ProductDateSortingUsingStreamAPI.java
package in.bench.resources.date.sorting;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class ProductDateSortingUsingStreamAPI {
public static void main(String[] args) throws ParseException {
// 1. products
Product product1 = new Product(1, "Cooker",
new SimpleDateFormat("yyyy-MM-dd").parse("2022-05-31"), 13750.0);
Product product2 = new Product(2, "Sofa-Bed",
new SimpleDateFormat("yyyy-MM-dd").parse("2019-11-02"), 11750.0);
Product product3 = new Product(3, "Fridge",
new SimpleDateFormat("yyyy-MM-dd").parse("2021-03-27"), 12750.0);
Product product4 = new Product(4, "Cupboard",
new SimpleDateFormat("yyyy-MM-dd").parse("2020-04-30"), 11525.0);
Product product5 = new Product(5, "Utensils",
new SimpleDateFormat("yyyy-MM-dd").parse("2022-06-01"), 17050.0);
// 1.1 List
List<Product> products = new ArrayList<Product>();
products.add(product1);
products.add(product2);
products.add(product3);
products.add(product4);
products.add(product5);
// 1.2 print to console
System.out.println("Before sorting :- \n");
products.stream().forEach(prod -> System.out.println(prod));
// 2. ascending-order sorting
List<Product> sortedProductListAsc = products
.stream()
.sorted(Comparator.comparing(Product::getProdCreatedDate))
.collect(Collectors.toList());
// 2.1 print to console
System.out.println("\n\nAscending-order sorting on the basis of Created Date :- \n");
sortedProductListAsc.stream().forEach(prod -> System.out.println(prod));
// 3. descending-order sorting
List<Product> sortedProductListDesc = products
.stream()
.sorted(Comparator.comparing(Product::getProdCreatedDate).reversed())
.collect(Collectors.toList());
// 3.1 print to console - descending order
System.out.println("\n\nDescending-order sorting on the basis of Created Date :- \n");
sortedProductListDesc.stream().forEach(prod -> System.out.println(prod));
}
}
Output:
Before sorting :-
Product [prodId=1, prodCreatedDate=Tue May 31 00:00:00 IST 2022, prodRate=13750.0, prodName=Cooker]
Product [prodId=2, prodCreatedDate=Sat Nov 02 00:00:00 IST 2019, prodRate=11750.0, prodName=Sofa-Bed]
Product [prodId=3, prodCreatedDate=Sat Mar 27 00:00:00 IST 2021, prodRate=12750.0, prodName=Fridge]
Product [prodId=4, prodCreatedDate=Thu Apr 30 00:00:00 IST 2020, prodRate=11525.0, prodName=Cupboard]
Product [prodId=5, prodCreatedDate=Wed Jun 01 00:00:00 IST 2022, prodRate=17050.0, prodName=Utensils]
Ascending-order sorting on the basis of Created Date :-
Product [prodId=2, prodCreatedDate=Sat Nov 02 00:00:00 IST 2019, prodRate=11750.0, prodName=Sofa-Bed]
Product [prodId=4, prodCreatedDate=Thu Apr 30 00:00:00 IST 2020, prodRate=11525.0, prodName=Cupboard]
Product [prodId=3, prodCreatedDate=Sat Mar 27 00:00:00 IST 2021, prodRate=12750.0, prodName=Fridge]
Product [prodId=1, prodCreatedDate=Tue May 31 00:00:00 IST 2022, prodRate=13750.0, prodName=Cooker]
Product [prodId=5, prodCreatedDate=Wed Jun 01 00:00:00 IST 2022, prodRate=17050.0, prodName=Utensils]
Descending-order sorting on the basis of Created Date :-
Product [prodId=5, prodCreatedDate=Wed Jun 01 00:00:00 IST 2022, prodRate=17050.0, prodName=Utensils]
Product [prodId=1, prodCreatedDate=Tue May 31 00:00:00 IST 2022, prodRate=13750.0, prodName=Cooker]
Product [prodId=3, prodCreatedDate=Sat Mar 27 00:00:00 IST 2021, prodRate=12750.0, prodName=Fridge]
Product [prodId=4, prodCreatedDate=Thu Apr 30 00:00:00 IST 2020, prodRate=11525.0, prodName=Cupboard]
Product [prodId=2, prodCreatedDate=Sat Nov 02 00:00:00 IST 2019, prodRate=11750.0, prodName=Sofa-Bed]
Related Articles:
- Java 8 – How to sort List by java.util.Date in different ways ?
- Java 8 – How to sort List by LocalDate in different ways ?
- Java 8 – How to sort List by LocalDateTime in different ways ?
- Java 8 – How to sort List by ZonedDateTime in different ways ?
- Java 8 – How to sort List by OffsetDateTime in different ways ?
- Java 8 – How to sort List by LocalTime in different ways ?
- Java 8 – How to sort List by OffsetTime in different ways ?
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/util/List.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html
Happy Coding !!
Happy Learning !!