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 listNumbers such thatLongs occur beforeIntegers.Only the specified
instanceOrderclasses are considered during comparison. If two objects are both instances of the ordered type this comparator will return a0. Consider combining with aCompoundComparatorif additional sorting is required.- Since:
- 3.2
- Author:
- Phillip Webb
- See Also:
CompoundComparator
Constructor Summary
Constructors Constructor Description InstanceComparator(Class<?>... instanceOrder)Create a newInstanceComparatorinstance.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompare(T o1, T o2)Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
Constructor Detail
InstanceComparator
public InstanceComparator(Class<?>... instanceOrder)
Create a newInstanceComparatorinstance.- Parameters:
instanceOrder- the ordered list of classes that should be used when comparing objects. Classes earlier in the list will be given a higher priority.