Originally posted by subir rastogi:
hi,i made some changes to your code and this is working now.You have override the method compareTo...
Actually, compareTo should return a negative value if this object is "less than" the object it's being compared to; zero if they are equal, and a positive value if this object is "greater than" the object it's being compared to.
In this case, you could simply return the difference of Person's int value...
Also from the API for Comparator...
It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals.
And finally, you will probably want to override the toSting method in Person, so that when you print your TreeSet you can verify the ordering.