org.springframework.util.comparator
Class InstanceComparator<T>
- java.lang.Object
-
- org.springframework.util.comparator.InstanceComparator<T>
-
- Type Parameters:
-
T
- The type of objects being compared
- All Implemented Interfaces:
- Comparator<T>
public class InstanceComparator<T> extends Object implements Comparator<T>
Compares objects based on an arbitrary class order. Allows objects to be sorted based on the types of class that they inherit, for example: this comparator can be used to sort a listNumber
s such thatLong
s occur beforeInteger
s.Only the specified
instanceOrder
classes are considered during comparison. If two objects are both instances of the ordered type this comparator will return a0
. Consider combining with aCompoundComparator
if additional sorting is required.- Since:
- 3.2
- Author:
- Phillip Webb
- See Also:
-
CompoundComparator
-
Constructor Summary
Constructors Constructor and Description InstanceComparator(Class<?>... instanceOrder)
Create a newInstanceComparator
instance.
-
Constructor Detail
InstanceComparator
public InstanceComparator(Class<?>... instanceOrder)
Create a newInstanceComparator
instance.- Parameters:
-
instanceOrder
- the ordered list of classes that should be used when comparing objects. Classes earlier in the list will be be given a higher priority.
Method Detail
compare
public int compare(T o1, T o2)
- Specified by:
-
compare
in interfaceComparator<T>