I understood the concept of Comparable interface and how it is used and the advantages of using comparator interface over comparable interface. But i am still confused that when we invoke the sort() method of Collections class Collections.sort(List l);
How did it know that type of List should implement comparable interface?
The list does not implement the Comparable interface - the class of the objects in the list must implement the Comparable interface.
The sort() method needs this, because it must be able to compare objects in the list to determine which comes before which in the sorted result.
The API documentation of the method Collections.sort() tells you this:
Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface.
[ August 09, 2007: Message edited by: Jesper Young ]