Issue
[The body has been removed by the user.]
Solution
Your basic implementation of Comparable
compares the names of the hills, it has nothing to do with the height
How can I adjust Comparable so that it allows me to sort by name and then by height after?
Create a new Comparator which implements the logic you need to compare the heights and use that with Collections.sort
Collections.sort(listOfHills, new Comparator<Hill>() {
@Override
public int compare(Hill o1, Hill o2) {
return o2.height > o1.height ? (o2.height == o1.height ? 0 : 1) : -1;
}
});
This is one of the reasons I don't normally implement Comparator
on objects, unless their is a good business rule associated with how the object should be compared, it's easily to provide a number of custom Comparator
s (or allow other developers to devise their own) which implement "common" algorithms - but that's me
Answered By - MadProgrammer
Answer Checked By - Marilyn (JavaFixing Volunteer)