Course – LS – All

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

>> CHECK OUT THE COURSE

1. Overview

When programming, we often need to sort collections of objects. The sorting logic can sometimes become difficult to implement if we want to sort the objects on multiple fields. In this tutorial, we’ll discuss several different approaches to the problem, along with their pros and cons.

2. Example Person Class

Let’s define a Person class with two fields, name and age. We’ll be comparing Person objects first based on name and then on age throughout our examples:

public class Person {
    @Nonnull private String name;
    private int age;

    // constructor
    // getters and setters
}

Here, we’ve added a @Nonnull annotation to keep the examples simple. But in production code, we may need to handle the comparison of nullable fields.

3. Use Comparator.compare()

Java provides the Comparator interface for comparing two objects of the same type. We can implement its compare(T o1, T o2) method with customized logic to perform the desired comparison.

3.1. Check Different Fields One by One

Let’s compare the fields one after another:

public class CheckFieldsOneByOne implements Comparator<Person> {
    @Override
    public int compare(Person o1, Person o2) {
        int nameCompare = o1.getName().compareTo(o2.getName());
        if(nameCompare != 0) {
            return nameCompare;
        }
        return Integer.compare(o1.getAge(), o2.getAge());
    }
}

Here, we use the String class’s compareTo() method and the Integer class’s compare() method to compare the name and age fields one after the other.

This requires a lot of typing, and sometimes handling of many special cases. Therefore, it’s hard to maintain and scale when we have more fields to compare. Generally, it’s not recommended to use this method in production code.

3.2. Use Guava’s ComparisonChain

First, let’s add the Google Guava library dependency to our pom.xml:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.1-jre</version>
</dependency>

We can simplify the logic by using the ComparisonChain class from this library:

public class ComparisonChainExample implements Comparator<Person> {
    @Override
    public int compare(Person o1, Person o2) {
        return ComparisonChain.start()
          .compare(o1.getName(), o2.getName())
          .compare(o1.getAge(), o2.getAge())
          .result();
    }
}

Here, we use the compare(int left, int right) and compare(Comparable<?> left, Comparable<?> right) methods in ComparisonChain to compare name and age, respectively.

This approach hides the comparison details and only exposes what we care about — the fields we’d like to compare and the order in which they should be compared. Also, we should note that we don’t need any additional logic for null handling as the library methods take care of it. Therefore, it becomes easier to maintain and scale.

3.3. Sorting With Apache Commons’ CompareToBuilder

First, let’s add the dependency for Apache Commons to the pom.xml:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

Similar to the previous example, we can use CompareToBuilder from Apache Commons to reduce the boilerplate code needed:

public class CompareToBuilderExample implements Comparator<Person> {
    @Override
    public int compare(Person o1, Person o2) {
        return new CompareToBuilder()
          .append(o1.getName(), o2.getName())
          .append(o1.getAge(), o2.getAge())
          .build();
    }
}

This approach is very similar to Guava’s ComparisonChain — it also hides the comparison details and is easily maintainable and scalable.

4. Use Comparator.comparing() and Lambda Expression

Since Java 8, there are several static methods added to the Comparator interface that can take lambda expressions to create a Comparator object. We can use its comparing() method to construct the Comparator we need:

public static Comparator<Person> createPersonLambdaComparator() {
    return Comparator.comparing(Person::getName)
      .thenComparing(Person::getAge);
}

This approach is much more concise and readable as it directly takes the getters of the Person class.

It also keeps the maintainability and scalability characteristics of the approaches we saw earlier. Additionally, the getters here are lazily evaluated, compared to the immediate evaluation in the previous approaches. As a result, its performance is better and more suitable for latency-sensitive systems that require a lot of large data comparisons.

Moreover, this approach only uses core Java classes and doesn’t require any third-party libraries as dependencies. Overall, this is the most recommended approach.

5. Examine the Comparison Results

Let’s test the four comparators we saw and inspect their behaviors. All these comparators can be invoked in the same way and should produce the same result:

@Test
public void testComparePersonsFirstNameThenAge() {
    Person person1 = new Person("John", 21);
    Person person2 = new Person("Tom", 20);
    // Another person named John
    Person person3 = new Person("John", 22);

    List<Comparator<Person>> comparators =
      Arrays.asList(new CheckFieldsOneByOne(),
        new ComparisonChainExample(),
        new CompareToBuilderExample(),
        createPersonLambdaComparator());
    // All comparators should produce the same result
    for(Comparator<Person> comparator : comparators) {
        Assertions.assertIterableEquals(
          Arrays.asList(person1, person2, person3)
            .stream()
            .sorted(comparator)
            .collect(Collectors.toList()),
          Arrays.asList(person1, person3, person2));
    }
}

Here, person1 has the same name (“John”) as person3, but is younger (21 < 22), while person3′s name (“John”) is lexicographically less than person2‘s name (“Tom”). So, the final ordering is person1, person3, person2.

Also, we should note that if we don’t have the @Nonnull annotation on the class variable name, we’d need to add extra logic to handle the null case in all the approaches except for Apache Commons’ CompareToBuilder (which has native null handling built in).

6. Conclusion

In this article, we learned different approaches for comparing on multiple fields when sorting collections of objects.

As always, the source code for the examples is available 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 closed on this article!