Would you give an example of “choose the natural ordering inside the class.”
I read Object Ordering in docs.oracle but it doesn’t say how the natural ordering is decided in the case where there are multiple elements. If a class has elements such as name, age, address, phone number; which one gets picked to do the natural ordering?
Then, I read in a post here that one can decide the natural ordering. I am a Java newbie trying to tackle a homework assignment and taking my first stab at the collections framework.
I prefer Comparator classes for this, even though the implementation looks a bit more heavyweight. The reason is that, for some objects, it’s not always clear what’s being compared. Using
.compareTo()(that is, implementing IComparable) doesn’t make it as clear as an explicit Comparator does.Here I use an anonymous class, but you could make the Comparators their own (named) class if you like.