my dog learned polymorphism*
The moose likes Java in General and the fly likes Collections.sort() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collections.sort()" Watch "Collections.sort()" New topic
Author

Collections.sort()

Prasanna Raman
Ranch Hand

Joined: Sep 05, 2010
Posts: 331
Hello,

I read that Collections.sort() will work when all elements of the collection implement the Comparable interface. Why does the below work even when I am not explicitly implementing Comparable? Am I understanding the spec wrong?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Every object which you add to your List is an Integer. And Integer implements Comparable (you could look that up in the documentation). Therefore... all elements of the collection implement the Comparable interface.

So yes, you're understanding the spec wrong. It's the part where you decided you had to provide your own Comparable implementation, without realizing that the writers of the standard API had already done that.
Prasanna Raman
Ranch Hand

Joined: Sep 05, 2010
Posts: 331
Thank you, Paul! Got it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections.sort()