This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 329
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: 329
Thank you, Paul! Got it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections.sort()
 
Similar Threads
illegal start of expression
Question On Comparator
Comparing Two ArrayLists
Collections.sort
Read file from directory, update contents of the each file