• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections.sort()

 
Prasanna Raman
Ranch Hand
Posts: 390
  • 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
Posts: 20980
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 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: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Paul! Got it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic