Sorting data in Java using Apache BeanUtils

Most data driven applications have a requirement to be able to sort data. Most of the time this can be accomplished at the data access layer by sorting the data when it is retrieved via SQL query, or by using JavaScript or some other scripting language to sort the data in the view layer. However, there are also times when it is convenient to sort a collection of POJOs in the business layer.This article discusses sorting POJOs in Java using the Apache BeanUtils libraries.

The Java API gives us a couple of ways to compare objects for sorting:

  • Implement the Comparable interface
  • Implement the Comparator interface

For more detail on using these interfaces, check out this article on Java sorting.

The Collections API also has its own internal sorting methods that sort by an object’s natural order (if no Comparator is provided), or by an object whose type extends the Comparator interface. In this example, we use the sort() method in the Collections API to sort our objects. Instead of using the clunky Java API to build our own Comparator implementation, we will use the Apache BeanUtils package.

Using Apache BeanUtils BeanComparator

Here’s the code for our POJO that we are going to sort (getters and setters omitted):

public class WorldsGreatestTVShow {
    private String actorName;
    private String producerName;
    private String directorName;
    private String location;
    //other code here
}

Here’s our action class in the business layer that performs the sorting:

import org.apache.commons.beanutils.BeanComparator;
...
public void sortStuff() {
    List<WorldsGreatestTVShow> tvShows = new ArrayList< WorldsGreatestTVShow>();
    //group (sort) by actor
    BeanComparator actorComparator = new BeanComparator("actorName");
    Collections.sort(tvShows, actorComparator);
    //group (sort) by producer
    BeanComparator producerComparator = new BeanComparator("producerName");
    Collections.sort(tvShows, producerComparator);
}

All we did was use the BeanComparator class to create a comparator on a property of the POJO. We passed the BeanComparator object as a parameter to the sort method in the Collections API. It doesn’t get much easier than that.

In contrast, here’s another approach to the same problem. This time, we’ll just use a Java Comparator directly instead of using the Apache Commons BeanComparator.

Alternate approach using Java Comparator

Our POJO will change slightly because we are building our own comparator:

import java.util.Comparator;
...
public class WorldsGreatestTVShow {
    private String actorName;
    private String producerName;
    private String directorName;
    private String location;
    public static final Comparator<WorldsGreatestTVShow> ACTOR_NAME_ORDER =
         new Comparator< WorldsGreatestTVShow>() {
             public int compare(WorldsGreatestTVShow obj1, WorldsGreatestTVShow obj2) {
                     return obj1.getActorName().compareTo(obj2.getActorName);
             }
         };
    //other code here
}

In our action class, we still use the Collections API to perform the sort. This time, we pass the static Comparator instance as a parameter:

public void sortStuff() {
    List<WorldsGreatestTVShow> tvShows = new ArrayList< WorldsGreatestTVShow>();
    //group (sort) by actor
    Collections.sort(tvShows, WorldsGreatestTVShow.ACTOR_NAME_ORDER);
}

The Apache Commons BeanComparator class is useful in many cases when you are sorting on standard data types. I prefer using it to building my own comparator in Java. Try it out and see what you think.

Tags:

Leave a Reply

You must be logged in to post a comment.