Sorting Objects in a List by Date – 按日期对列表中的对象进行排序

最后修改: 2022年 5月 31日

1. Overview


In this tutorial, we’ll discuss sorting objects in a List by date. Most of the sorting techniques or examples let the user sort a list alphabetically, but in this article, we’ll discuss how to do it with Date objects.


We’ll look at using Java’s Comparator class for custom sorting our lists’ values.


2. Setup


Let’s look at the Employee entity we’ll be using in this article:


public class Employee implements Comparable<Employee> {

    private String name;
    private Date joiningDate;

    public Employee(String name, Date joiningDate) {
        // ...

    // standard getters and setters

We can notice that we have implemented a Comparable interface in the Employee class. This interface lets us define a strategy for comparing objects with other objects of the same type. This is used to sort the objects in their natural ordering form or defined by the compareTo() method.


3. Sorting Using Comparable


In Java, natural order refers to how we should sort primitives or objects in an array or collection. The sort() method in java.util.Arrays and java.util.Collections should be consistent and reflect the semantics of equality.


We’ll use this method for comparing the current object and the object passed as an argument:


public class Employee implements Comparable<Employee> {
    // ...

    public boolean equals(Object obj) {
        return ((Employee) obj).getName().equals(getName());

    public int compareTo(Employee employee) {
        return getJoiningDate().compareTo(employee.getJoiningDate());

This compareTo() method will be comparing the current object with the object that is being sent as a parameter. In the above example, we compare the joining date of the current object with the passed Employee object.


3.1. Sorting in Ascending Order


In most cases, the compareTo() method describes the logic for comparison among objects with natural sorting. Here, we compare the employee’s joining date field with other objects of the same type. Any two employees will return 0 if they have the same joining date:


public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() {
    assertEquals(employees, employeesSortedByDateAsc);

Now, the Collections.sort(employees) will sort the employee list based on its joiningDate instead of its primary key or name. We can see the list is sorted by joiningDate of the employees – that now becomes a natural order for Employee class:


[(Pearl,Tue Apr 27 23:30:47 IST 2021),
(Earl,Sun Feb 27 23:30:47 IST 2022),
(Steve,Sun Apr 17 23:30:47 IST 2022),
(John,Wed Apr 27 23:30:47 IST 2022)]

3.2. Sorting in Descending Order


The Collections.reverseOrder() method sort the objects but in the reverse order as imposed by the natural ordering. This returns a comparator that will perform the ordering in reverse. It’ll throw a NullPointerException when the object returns null on the comparison:


public void givenEmpList_SortEmpList_thenSortedListinDescOrder() {
    Collections.sort(employees, Collections.reverseOrder());
    assertEquals(employees, employeesSortedByDateDesc);

4. Sorting Using Comparator


4.1. Sorting in Ascending Order


Let’s now use the Comparator interface implementation to sort our employee list. Here, we’ll pass an anonymous inner class parameter on the on-the-fly to the Collections.sort() API:

现在让我们使用Comparator接口实现来对我们的雇员列表进行排序。在这里,我们将向Collections.sort() API即时传递一个匿名的内部类参数。

public void givenEmpList_SortEmpList_thenCheckSortedList() {

    Collections.sort(employees, new Comparator<Employee>() {
        public int compare(Employee o1, Employee o2) {
            return o1.getJoiningDate().compareTo(o2.getJoiningDate());

    assertEquals(employees, employeesSortedByDateAsc);

We can also replace this syntax with the Java 8 Lambda Syntax that makes our code much smaller, as shown below:

我们也可以用Java 8 Lambda语法代替这种语法,使我们的代码更小,如下所示。

public void givenEmpList_SortEmpList_thenCheckSortedListAscLambda() {

    Collections.sort(employees, Comparator.comparing(Employee::getJoiningDate));

    assertEquals(employees, employeesSortedByDateAsc);

The compare(arg1, arg2) method takes two arguments of the generic type and returns an integer. Since it is separated from the class definition, we can define a custom comparison based on different variables and entities. This is useful when we want to define a different custom sort for comparing the argument objects.

compare(arg1, arg2)方法接收两个通用类型的参数并返回一个整数。由于它与类的定义分离,我们可以根据不同的变量和实体定义一个自定义的比较。当我们想定义一个不同的自定义排序来比较参数对象时,这很有用。

4.2. Sorting in Descending Order


We can sort the given Employee list in descending order by reversing the employee object comparison, i.e., comparing Employee2 with Employee1. This will reverse the comparison and thus return the result in descending order:


public void givenEmpList_SortEmpList_thenCheckSortedListDescV1() {

    Collections.sort(employees, new Comparator<Employee>() {
        public int compare(Employee emp1, Employee emp2) {
            return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());

    assertEquals(employees, employeesSortedByDateDesc);

We can also convert the above method to more concise forms using the Java 8 Lambda Expressions. This will perform the same functions as the above function, with the only difference that the code contains fewer lines of code in comparison to the above code. Though this also makes the code less readable. While using Comparator, we pass an anonymous inner class on-the-fly for the Collections.sort() API:

我们也可以使用Java 8 Lambda Expressions将上述方法转换成更简洁的形式。这将执行与上述函数相同的功能,唯一不同的是,与上述代码相比,代码包含的行数更少。尽管这也使得代码的可读性降低。在使用比较器时,我们为Collections.sort() API实时传递一个匿名的内部类。

public void givenEmpList_SortEmpList_thenCheckSortedListDescLambda() {

    Collections.sort(employees, (emp1, emp2) -> emp2.getJoiningDate().compareTo(emp1.getJoiningDate()));
    assertEquals(employees, employeesSortedByDateDesc);

5. Conclusion


In this article, we explored how to sort a Java Collection by Date object in both ascending and descending modes.

在这篇文章中,我们探讨了如何通过Date对象以升序和降序模式对Java Collection进行排序。

We also briefly saw the Java 8 lambda features that are useful in sorting and help in making the code concise.

我们还简要地看到了Java 8的lambda功能,这些功能在排序中很有用,有助于使代码简洁。

As always, the complete code examples used in this article can be found over on GitHub.