File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about Comparator

 
Brian Legg
Ranch Hand
Posts: 488
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read something interesting saying that if your object is not modifiable (final) then you must use Comparator to sort them in a collection.

Why can't you use Comparable? Is it as simple as the final object can't implement Comparable, or is there something going on behind the scenes I should know?

Thanks!
 
patrick avery
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is because Comparator is implemented as a separate class so you don't need to be able to modify the object being sorted, whereas Comparable must be implemented as a method in the class being sorted...hard to do if you can't modify the object.
 
Brian Legg
Ranch Hand
Posts: 488
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True true... I always just assume that whatever object you are going to sort is known upfront and/or you can change the code.

Obviously that is a bad assumption. Thanks, makes since!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic