In Java, it is straightforward to sort objects of the predefined class such as Integer, Double, Float and String. This is because these classes have been implemented the Comparable interface. Comparable defines a natural ordering, as when you’re defining it, you specify which one is considered “less than” or “greater than” the another.
How can you sort custom Objects? For example, suppose you have a list of objects of Student class, which has name, age, and score attributes. You may want to sort the students by their scores. Another example is to sort a list strings by their length.
[Read More...]