• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections comparable interface

 
Shaveta Chawla
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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?

thanks
Shaveta
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic