Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

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> {
    
    // ...

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

    @Override
    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:

@Test
public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() {
    Collections.sort(employees);
    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:

@Test
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:

@Test
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:

@Test
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.

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:

@Test
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:

@Test
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.

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

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

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.