Tuesday, July 31, 2007 05:23 PM
You should read the detailed description provided in the javadoc for the Comparable
Basically, implementing Comparable allows you to explicitly specify the "natural ordering" for a class.
Then you can sort it with Collections.sort(List).
Notice that the Comparator no longer needs to be specified because you've moved the compareTo() into your class.
Note that if you implement Comparable, you should also provide equals() and hashCode() methods in your class that are consistent with your compareTo(). In other words calling your class' equals() should always give true when your class' compareTo() returns 0. And hashCode() results for instances of your class should match where equals() is true.
I hope I said that last part right! Read the Comparable javadoc.