views:

4119

answers:

7

I have seen both used. When would you use one over the other?

+12  A: 

The text below comes from http://lkamal.blogspot.com/2008/07/java-sorting-comparator-vs-comparable.html

Comparable

A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances.

Comparator

A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.lang.Comparator interface.

Dan
+4  A: 

Comparable is for providing a default ordering on data objects.

A comparator represents the ordering itself for a specific use.

starblue
+4  A: 

Implementing Comparable means "I can compare myself with another object." This is typically useful when there's a single natural default comparison.

Implementing Comparator means "I can compare two other objects." This is typically useful when there are multiple ways of comparing two instances of a type - e.g. you could compare people by age, name etc.

Jon Skeet
+2  A: 

Comparable is usually preferred. But sometimes a class already implements Comparable, but you want to sort on a different property. Then you're forced to use a Comparator.

Some classes actually provide Comparators for common cases; for instance, Strings are by default case-sensitive when sorted, but there is also a static Comparator called CASE_INSENSITIVE_ORDER.

Michael Myers
I'm not sure I agree with Comparable being preferred. Some objects have a strong sense of natural order—namely numbers, in all their forms: natural numbers, real numbers, dates, etc. But even other relatively primitive objects like character strings lack a universally applicable order. In the case of more complex objects like an entity from an application domain model, implementing Comparable is usually a mistake. Their many properties make it too difficult to anticipate what order will be wanted most often.
erickson
+1  A: 

Comparable is for objects with a natural ordering. The object itself knows how it is to be ordered. Comparator is for objects without a natural ordering or when you wish to use a different ordering.

mlk
+10  A: 

Comparable lets a class implement its own comparison:

  • it's in the same class (it is often an advantage)
  • there can be only one implementation (so you can't use that if you want two different cases)

By comparison, Comparator is an external comparison:

  • it is typically in a unique instance (either in the same class or in another place)
  • you name each implementation with the way you want to sort things
  • you can provide comparators for classes that you do not control
  • the implementation is usable even if the first object is null


In both implementations, you can still choose to what you want to be compared. With generics, you can declare so, and have it checked at compile-time. This improves safety, but it is also a challenge to determine the appropriate value.

As a guideline, I generally use the most general class or interface to which that object could be compared, in all use cases I envision... Not very precise a definition though ! :-(

  • Comparable<Object> lets you use it in all codes at compile-time (which is good if needed, or bad if not and you loose the compile-time error) ; your implementation has to cope with objects, and cast as needed but in a robust way.
  • Comparable<Itself> is very strict on the contrary.

Funny, when you subclass Itself to Subclass, Subclass must also be Comparable and be robust about it (or it would break Liskov Principle, and give you runtime errors).

KLE
@Rich Seller Thanks for correcting typo.
KLE
A: 

Diffrence between Comparator and Comparable interfaces

Comparable is used to compare itself by using with another object.

Comparator is used to compare two datatypes are objects.

SravaniChowdary.Gorijavolu