In this article, we will discuss and learn how to sort List by java.time.LocalTime in ascending as well as descending order in different ways
Sort List by LocalTime 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.time.LocalTime in ascending and descending order in 4 different ways as mentioned above.
Product.java
package in.bench.resources.localtime.sorting;
import java.time.LocalTime;
public class Product {
// member variables
private int prodId;
private String prodName;
private LocalTime prodCreatedTime;
private double prodRate;
// getters and setters
// 4-arg parameterized constructor
// override toString() method
@Override
public String toString() {
return "Product [prodId=" + prodId
+ ", prodRate=" + prodRate
+ ", prodCreatedTime=" + prodCreatedTime
+ ", prodName=" + prodName
+ "]";
}
}
1. Sort List by LocalTime using java.util.Comparator :
- There is a list of Products and we will be sorting product list by LocalTime 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 of Comparator
- Finally, we will print product list to console in ascending and descending order
ProductSortingComparator.java
package in.bench.resources.localtime.sorting;
import java.util.Comparator;
public class ProductSortingComparator implements Comparator<Product> {
@Override
public int compare(Product prod1, Product prod2) {
return prod1.getProdCreatedTime().compareTo(prod2.getProdCreatedTime());
}
}
ProductLocalTimeSortingUsingComparator.java
package in.bench.resources.localtime.sorting;
import java.text.ParseException;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ProductLocalTimeSortingUsingComparator {
public static void main(String[] args) throws ParseException {
// 1. products
Product product1 = new Product(1, "Cooker",
LocalTime.of(20, 28, 57, 123), 13750.0);
Product product2 = new Product(2, "Sofa-Bed",
LocalTime.of(13, 29, 59, 456), 11750.0);
Product product3 = new Product(3, "Fridge",
LocalTime.of(16, 14, 54, 789), 12750.0);
Product product4 = new Product(4, "Cupboard",
LocalTime.of(8, 29, 19, 987), 11525.0);
Product product5 = new Product(5, "Utensils",
LocalTime.of(17, 17, 11, 654), 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 LocalTime :- \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 LocalTime :- \n");
for(Product prod : products) {
System.out.println(prod);
}
}
}
Output:
Before sorting :-
Product [prodId=1, prodRate=13750.0, prodCreatedTime=20:28:57.000000123, prodName=Cooker]
Product [prodId=2, prodRate=11750.0, prodCreatedTime=13:29:59.000000456, prodName=Sofa-Bed]
Product [prodId=3, prodRate=12750.0, prodCreatedTime=16:14:54.000000789, prodName=Fridge]
Product [prodId=4, prodRate=11525.0, prodCreatedTime=08:29:19.000000987, prodName=Cupboard]
Product [prodId=5, prodRate=17050.0, prodCreatedTime=17:17:11.000000654, prodName=Utensils]
Ascending-order sorting on the basis of LocalTime :-
Product [prodId=4, prodRate=11525.0, prodCreatedTime=08:29:19.000000987, prodName=Cupboard]
Product [prodId=2, prodRate=11750.0, prodCreatedTime=13:29:59.000000456, prodName=Sofa-Bed]
Product [prodId=3, prodRate=12750.0, prodCreatedTime=16:14:54.000000789, prodName=Fridge]
Product [prodId=5, prodRate=17050.0, prodCreatedTime=17:17:11.000000654, prodName=Utensils]
Product [prodId=1, prodRate=13750.0, prodCreatedTime=20:28:57.000000123, prodName=Cooker]
Descending-order sorting on the basis of LocalTime :-
Product [prodId=1, prodRate=13750.0, prodCreatedTime=20:28:57.000000123, prodName=Cooker]
Product [prodId=5, prodRate=17050.0, prodCreatedTime=17:17:11.000000654, prodName=Utensils]
Product [prodId=3, prodRate=12750.0, prodCreatedTime=16:14:54.000000789, prodName=Fridge]
Product [prodId=2, prodRate=11750.0, prodCreatedTime=13:29:59.000000456, prodName=Sofa-Bed]
Product [prodId=4, prodRate=11525.0, prodCreatedTime=08:29:19.000000987, prodName=Cupboard]
2. Sort List by LocalTime using Lambda Expression :
- There is a list of Products and we will be sorting product list by LocalTime 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 time 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 of Comparator and pass actual list and reversed Comparator to Collections.sort() method
- Finally, we will print product list to console in ascending and descending order
ProductLocalTimeSortingUsingLambdaExpression.java
package in.bench.resources.localtime.sorting;
import java.text.ParseException;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ProductLocalTimeSortingUsingLambdaExpression {
public static void main(String[] args) throws ParseException {
// 1. products
Product product1 = new Product(1, "Cooker",
LocalTime.of(20, 28, 57, 123), 13750.0);
Product product2 = new Product(2, "Sofa-Bed",
LocalTime.of(13, 29, 59, 456), 11750.0);
Product product3 = new Product(3, "Fridge",
LocalTime.of(16, 14, 54, 789), 12750.0);
Product product4 = new Product(4, "Cupboard",
LocalTime.of(8, 29, 19, 987), 11525.0);
Product product5 = new Product(5, "Utensils",
LocalTime.of(17, 17, 11, 654), 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) -> prod1.getProdCreatedTime()
.compareTo(prod2.getProdCreatedTime());
// 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 LocalTime :- \n");
products.forEach(prod -> System.out.println(prod));
// 3. get Comparator using Lambda expression
Comparator<Product> comparatorDesc = (prod1, prod2) -> prod2.getProdCreatedTime()
.compareTo(prod1.getProdCreatedTime());
// 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 LocalTime :- \n");
products.forEach(prod -> System.out.println(prod));
}
}
Output:
Before sorting :-
Product [prodId=1, prodRate=13750.0, prodCreatedTime=20:28:57.000000123, prodName=Cooker]
Product [prodId=2, prodRate=11750.0, prodCreatedTime=13:29:59.000000456, prodName=Sofa-Bed]
Product [prodId=3, prodRate=12750.0, prodCreatedTime=16:14:54.000000789, prodName=Fridge]
Product [prodId=4, prodRate=11525.0, prodCreatedTime=08:29:19.000000987, prodName=Cupboard]
Product [prodId=5, prodRate=17050.0, prodCreatedTime=17:17:11.000000654, prodName=Utensils]
Ascending-order sorting on the basis of LocalTime :-
Product [prodId=4, prodRate=11525.0, prodCreatedTime=08:29:19.000000987, prodName=Cupboard]
Product [prodId=2, prodRate=11750.0, prodCreatedTime=13:29:59.000000456, prodName=Sofa-Bed]
Product [prodId=3, prodRate=12750.0, prodCreatedTime=16:14:54.000000789, prodName=Fridge]
Product [prodId=5, prodRate=17050.0, prodCreatedTime=17:17:11.000000654, prodName=Utensils]
Product [prodId=1, prodRate=13750.0, prodCreatedTime=20:28:57.000000123, prodName=Cooker]
Descending-order sorting on the basis of LocalTime :-
Product [prodId=1, prodRate=13750.0, prodCreatedTime=20:28:57.000000123, prodName=Cooker]
Product [prodId=5, prodRate=17050.0, prodCreatedTime=17:17:11.000000654, prodName=Utensils]
Product [prodId=3, prodRate=12750.0, prodCreatedTime=16:14:54.000000789, prodName=Fridge]
Product [prodId=2, prodRate=11750.0, prodCreatedTime=13:29:59.000000456, prodName=Sofa-Bed]
Product [prodId=4, prodRate=11525.0, prodCreatedTime=08:29:19.000000987, prodName=Cupboard]
3. Sort List by LocalTime using Method References :
- There is a list of Products and we will be sorting product list by LocalTime 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 of Comparator and pass reversed Comparator to List.sort() method
- Finally, we will print product list to console in ascending and descending order
ProductLocalTimeSortingUsingMethodReferences.java
package in.bench.resources.localtime.sorting;
import java.text.ParseException;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class ProductLocalTimeSortingUsingMethodReferences {
public static void main(String[] args) throws ParseException {
// 1. products
Product product1 = new Product(1, "Cooker",
LocalTime.of(20, 28, 57, 123), 13750.0);
Product product2 = new Product(2, "Sofa-Bed",
LocalTime.of(13, 29, 59, 456), 11750.0);
Product product3 = new Product(3, "Fridge",
LocalTime.of(16, 14, 54, 789), 12750.0);
Product product4 = new Product(4, "Cupboard",
LocalTime.of(8, 29, 19, 987), 11525.0);
Product product5 = new Product(5, "Utensils",
LocalTime.of(17, 17, 11, 654), 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::getProdCreatedTime));
// 2.1 print to console
System.out.println("\n\nAscending-order sorting on the basis of LocalTime :- \n");
products.forEach(System.out::println);
// 3. descending-order sorting using Method References
products.sort(Comparator.comparing(Product::getProdCreatedTime).reversed());
// 3.1 print to console - descending order
System.out.println("\n\nDescending-order sorting on the basis of LocalTime :- \n");
products.forEach(System.out::println);
}
}
Output:
Before sorting :-
Product [prodId=1, prodRate=13750.0, prodCreatedTime=20:28:57.000000123, prodName=Cooker]
Product [prodId=2, prodRate=11750.0, prodCreatedTime=13:29:59.000000456, prodName=Sofa-Bed]
Product [prodId=3, prodRate=12750.0, prodCreatedTime=16:14:54.000000789, prodName=Fridge]
Product [prodId=4, prodRate=11525.0, prodCreatedTime=08:29:19.000000987, prodName=Cupboard]
Product [prodId=5, prodRate=17050.0, prodCreatedTime=17:17:11.000000654, prodName=Utensils]
Ascending-order sorting on the basis of LocalTime :-
Product [prodId=4, prodRate=11525.0, prodCreatedTime=08:29:19.000000987, prodName=Cupboard]
Product [prodId=2, prodRate=11750.0, prodCreatedTime=13:29:59.000000456, prodName=Sofa-Bed]
Product [prodId=3, prodRate=12750.0, prodCreatedTime=16:14:54.000000789, prodName=Fridge]
Product [prodId=5, prodRate=17050.0, prodCreatedTime=17:17:11.000000654, prodName=Utensils]
Product [prodId=1, prodRate=13750.0, prodCreatedTime=20:28:57.000000123, prodName=Cooker]
Descending-order sorting on the basis of LocalTime :-
Product [prodId=1, prodRate=13750.0, prodCreatedTime=20:28:57.000000123, prodName=Cooker]
Product [prodId=5, prodRate=17050.0, prodCreatedTime=17:17:11.000000654, prodName=Utensils]
Product [prodId=3, prodRate=12750.0, prodCreatedTime=16:14:54.000000789, prodName=Fridge]
Product [prodId=2, prodRate=11750.0, prodCreatedTime=13:29:59.000000456, prodName=Sofa-Bed]
Product [prodId=4, prodRate=11525.0, prodCreatedTime=08:29:19.000000987, prodName=Cupboard]
4. Sort List by LocalTime using Stream API :
- There is a list of Products and we will be sorting product list by java.time.LocalTime 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 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
ProductLocalTimeSortingUsingStreamAPI.java
package in.bench.resources.localtime.sorting;
import java.text.ParseException;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class ProductLocalTimeSortingUsingStreamAPI {
public static void main(String[] args) throws ParseException {
// 1. products
Product product1 = new Product(1, "Cooker",
LocalTime.of(20, 28, 57, 123), 13750.0);
Product product2 = new Product(2, "Sofa-Bed",
LocalTime.of(13, 29, 59, 456), 11750.0);
Product product3 = new Product(3, "Fridge",
LocalTime.of(16, 14, 54, 789), 12750.0);
Product product4 = new Product(4, "Cupboard",
LocalTime.of(8, 29, 19, 987), 11525.0);
Product product5 = new Product(5, "Utensils",
LocalTime.of(17, 17, 11, 654), 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::getProdCreatedTime))
.collect(Collectors.toList());
// 2.1 print to console
System.out.println("\n\nAscending-order sorting on the basis of LocalTime :- \n");
sortedProductListAsc.stream().forEach(prod -> System.out.println(prod));
// 3. descending-order sorting
List<Product> sortedProductListDesc = products
.stream()
.sorted(Comparator.comparing(Product::getProdCreatedTime).reversed())
.collect(Collectors.toList());
// 3.1 print to console - descending order
System.out.println("\n\nDescending-order sorting on the basis of LocalTime :- \n");
sortedProductListDesc.stream().forEach(prod -> System.out.println(prod));
}
}
Output:
Before sorting :-
Product [prodId=1, prodRate=13750.0, prodCreatedTime=20:28:57.000000123, prodName=Cooker]
Product [prodId=2, prodRate=11750.0, prodCreatedTime=13:29:59.000000456, prodName=Sofa-Bed]
Product [prodId=3, prodRate=12750.0, prodCreatedTime=16:14:54.000000789, prodName=Fridge]
Product [prodId=4, prodRate=11525.0, prodCreatedTime=08:29:19.000000987, prodName=Cupboard]
Product [prodId=5, prodRate=17050.0, prodCreatedTime=17:17:11.000000654, prodName=Utensils]
Ascending-order sorting on the basis of LocalTime :-
Product [prodId=4, prodRate=11525.0, prodCreatedTime=08:29:19.000000987, prodName=Cupboard]
Product [prodId=2, prodRate=11750.0, prodCreatedTime=13:29:59.000000456, prodName=Sofa-Bed]
Product [prodId=3, prodRate=12750.0, prodCreatedTime=16:14:54.000000789, prodName=Fridge]
Product [prodId=5, prodRate=17050.0, prodCreatedTime=17:17:11.000000654, prodName=Utensils]
Product [prodId=1, prodRate=13750.0, prodCreatedTime=20:28:57.000000123, prodName=Cooker]
Descending-order sorting on the basis of LocalTime :-
Product [prodId=1, prodRate=13750.0, prodCreatedTime=20:28:57.000000123, prodName=Cooker]
Product [prodId=5, prodRate=17050.0, prodCreatedTime=17:17:11.000000654, prodName=Utensils]
Product [prodId=3, prodRate=12750.0, prodCreatedTime=16:14:54.000000789, prodName=Fridge]
Product [prodId=2, prodRate=11750.0, prodCreatedTime=13:29:59.000000456, prodName=Sofa-Bed]
Product [prodId=4, prodRate=11525.0, prodCreatedTime=08:29:19.000000987, prodName=Cupboard]
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/time/LocalTime.html
- https://docs.oracle.com/javase/8/docs/api/java/util/List.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.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 !!