Posted by Rob Camick on October 23, 2008
Java provides basic support for sorting Beans stored in Lists and Arrays by using the Collections.sort(…) and Arrays.sort(…) methods respectively. If you want your Bean to be sortable, then your Bean must implement the Comparable interface. This will provide your Bean with a “Natural Order” sort. What do you do when you want to sort your Bean on a different property? Well, in this case you will need to create a custom Comparator to be used by the sort methods. But, do we really need to create custom Comparators for each sortable property, or is there an easier way?
Welcome to the world of reflection. Using reflection we can create a BeanComparator that will allow us to sort on any property that has a zero parameter method that returns the value of the property. In other words methods like getFirstName(), getAge(). The BeanComparator will then do a natural order sort on that property. For getFirstName(), a sort on the String will be done. For getAge(), a sort on the Integer will be done. In this case, the reflection API will return the wrapper class of the primitive “int” type.
Using the BeanComparator you will also be able to set a few properties to control the sort:
- ascending (the default) or descending
- ignore case (the default) or use case. This property is only used when sorting Strings
- nulls last (the default) or nulls first
A simple example? Lets assume you have a Person class with the some properties accessed by the following methods:
To sort a group of people by age you could do the following:
BeanComparator bc = new BeanComparator(Person.class, "getAge");
The BeanComparator can also be used with the GroupComparator (discussed in an earlier post) to provide sorting on multiple properties at the same time.
It should be noted that by using reflection you will take a bit of a performance hit. Using a collection of 10k people the BeanComparator based sort took about 400ms, whereas the natural order sort took about 16ms. In this case you may want to create a custom Comparator to improve performance. A custom Comparator for our sample Person class with a getAge() method might look like:
public class PersonAgeComparator implements Comparator<Person>
public int compare(Person p1, Person p2)
return p1.getAge() - p2.getAge();
Collections.sort(people, new PersonAgeComparator());
As you can see it is not a lot of work to create the custom Comparator.
Finally, as mentioned earlier, you can also implement a “natural order” sort on the age property. The basic code added to the Person class would be:
class Person implements Comparable<Person>
public int compareTo(Person person)
return getAge() - person.getAge();
You can only have a single natural sort order but you can use as many custom Comparators or BeanComparators as you wish.