public class BeanComparator
extends java.lang.Object
implements java.util.Comparator<java.lang.Object>
A comparator which compares objects based on one or more bean properties. Nested properties
are fully supported. If a property is non-String and implements Comparable
then the
compareTo()
method is delegated to. Otherwise the property is converted to a String
and a Locale
aware Collator
is used to to compare property values.
Constructor and Description |
---|
BeanComparator(java.util.Locale locale,
java.lang.String... properties)
Constructs a BeanComparator for comparing beans based on the supplied set of properties,
using the supplied Locale to collate Strings.
|
BeanComparator(java.lang.String... properties)
Constructs a BeanComparator for comparing beans based on the supplied set of properties,
using the default system Locale to collate Strings.
|
Modifier and Type | Method and Description |
---|---|
int |
compare(java.lang.Object o1,
java.lang.Object o2)
Compares two JavaBeans for order.
|
public BeanComparator(java.lang.String... properties)
properties
- one or more property names to be used, in order, to sort beanspublic BeanComparator(java.util.Locale locale, java.lang.String... properties)
locale
- the Locale to be used for collating Stringsproperties
- one or more property names to be used, in order, to sort beanspublic int compare(java.lang.Object o1, java.lang.Object o2)
Compares two JavaBeans for order. Returns a negative integer, zero, or a positive integer as the first argument sorts earlier, equal to, or later than the second.
Iterates through the properties supplied in the constructor comparing the values of each property for the two beans. As soon as a property is found that supplied a non-equal ordering, the ordering is returned. If all properties are equal, will return 0.
compare
in interface java.util.Comparator<java.lang.Object>
o1
- the first object to be compared, must not be null.o2
- the second object to be compared, must not be null.java.lang.ClassCastException
- if the arguments' types, or the types of the properties,
prevent them from being compared by this Comparator.? Copyright 2005-2006, Stripes Development Team.