How to sort a list of generic types in Java
Use an intersection type, like this:
public class MyList<T extends BaseEntity & Comparable<T>> {...}
That specifies that T must be both a BaseEntity
and Comparable
to itself.
Don't use Collections.sort(List<T>)
, use Collections.sort(Lst<T>, Comparator<? extends T>)
instead. Write the comparation code in the comparator.