public class PersonNameComparator
implements Comparator
{
public int compare(Object obj1, Object obj2)
{
Person person1 = (Person) obj1;
Person person2 = (Person) obj2;
int result = person1.getLastName()
.compareTo(person2.getLastName());
if (result == 0)
{
result = person1.getFirstName()
.compareTo(person2.getFirstName());
}
if (result == 0)
{
result = person1.getMiddleInitial()
.compareTo(person2.getMiddleInitial());
}
return result;
}
}
Collections.sort(peopleToSort, new PersonNameComparator());
|
CollationExpressionBuilder
orderBy = CollectionUtils.newCollationExpressionBuilder();
orderBy.attribute("lastName")
.attribute("firstName")
.attribute("middleInitial");
Collections.sort(peopleToSort, orderBy.getExpression());
Or, call Collections.sort() indirectly by simply evaluating the expression...
orderBy.evaluate(peopleToSort);
|