File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections.sort()

 
Prasanna Raman
Ranch Hand
Posts: 374
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20163
24
MySQL Database
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 374
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Paul! Got it!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic