Sort A List of Objects
As a Java programmer, I often need to sort a list of objects. If the object is a primitive type, such asString, Integer, Long, Float, Double, or Date,
it is easy to use Collections.sort(). However, it is not easy to sort a list of user defined objects, which do not implement the Comparable
interface, for example, a Person object, as defined below. If you want to sort a list of Person objects by id
in ascending order, you hav to provide a Comparator class to encapsulate the ordering. One possible comparator class is defined below.
Instead, a generic comparator class is defined in this post to sort lists of primitive as well as user defined objects, in any specified order and by any specified field(s).
Read More...