Dorothy Taylor wrote:So if we need to sort these Person objects based on the natural ordering of each person’s complete name, then is it ok to just extend Comparator interface and implement the compare() method?
Dorothy,
You cannot
extend interface in class, you can
implement it. If you tried your idea (which I strongly suggest) then you already noticed it won't work. Not every Map implementation cares about key ordering, you have to choose the one which does (ie. TreeMap).
TreeMap javadoc stands:
The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
So you can either implement Comparable (not Comparator) interface in Person class, or create implementation of Comparator and provide it during initialization of map. You would also want to read about
Collator if you have to care about national characters in names, like 'é', 'ą', 'ö' etc.