To sort a list of objects by date in Java, we need to make use of the Collections.sort() method along with a custom Comparator.
Sort by LocalDate:
Collections.sort(employees, Comparator.comparing(Employee::getDateOfBirth));
Example:
package org.code2care.examples;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Employee {
private String name;
private LocalDate dateOfBirth;
public Employee(String name, LocalDate dateOfBirth) {
this.name = name;
this.dateOfBirth = dateOfBirth;
}
public String getName() {
return name;
}
public LocalDate getDateOfBirth() {
return dateOfBirth;
}
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("Mike", LocalDate.of(1990, 4, 1)));
employees.add(new Employee("Sam", LocalDate.of(1985, 6, 5)));
employees.add(new Employee("Andy", LocalDate.of(1980, 9, 20)));
employees.add(new Employee("Andrew", LocalDate.of(1950, 12, 25)));
employees.add(new Employee("Eric", LocalDate.of(1995, 11, 12)));
Collections.sort(employees, Comparator.comparing(Employee::getDateOfBirth));
for (Employee employee : employees) {
System.out.println(employee.getName() + " - DoB: " + employee.getDateOfBirth());
}
}
}
Output:
.jpg)
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle. - Java
- How to Show Line Numbers in Jupyter Notebook Cells - Python
- List of Java Simple Date Formats (Cheatsheet) - Java
- How to install Android Studio Chipmunk and SDK tools on macOS (2021.2) - Android-Studio
- How to fix bash ping command not found error - Bash
- bash: command not found error [macOS Terminal Linux, Unix or Windows] - MacOS
- Add Line Break (New Line) in Jupyter Notebook Markup Cell - Python
- How to Update Google Chrome Browser on Mac? - Chrome