Comparable : Java Glossary

go to home page C words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish 2008-08-05 by Roedy Green ©1996-2008 Canadian Mind Products
index page for letter ⇒ punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
Comparable
java.lang.Comparable is used to define the natural sort order of a class. In contrast, java.lang.Comparator is used to define an auxilliary sort order for a class.
How do you remember that java.util. Comparable. compareTo defines a natural order where java.util. Comparator. compare defines an auxiliary order? I think about it this way:

“Does it makes sense to compare two Oranges? If so they are Comparable. I will need a method that compares this Orange Object to (compareTo) another. If I want do define an alternate order, I need to invent some sort of independent machine, a Comparator to do it. The comparison is not inherent to Oranges, so there is no this. Instead, I compare two Orange Objects.”

Normally you also redefine equals when you implement Comparable even though it is not part of the Comparable interface. There are no generics involved in overriding equals.

To implement Comparable you must write only one method: compareTo. It is typically added to an existing class to describe the natural order of that class. compareTo compares this object with another.

public int compareTo( Object o );
Here is a typical compareTo routine, one used in BigDate to compare two dates. It defines the natural ordering of BigDate objects. It compares the ordinal field in this BigDate object with another. Make sure your method signature uses Object, not BigDate.

Beware of using subtraction in writing Comparabless rather than < > if there is any possibility you will be using numbers large enough to overflow and give the wrong sign comparing them with subtraction.

Comparable Using Generics

Comparable Gotcha

If you have a class:
// base class
class SalesTaxItem implements Comparable<SalesTaxItem>
{
...
}
and you try to extend it with:
you will get this error message. Comparable cannot be inherited with different arguments

The best way I know of to bypass the problem is to write both classes without the implements Comparable. Then write two Comparators instead.

Descending/Inverse/Reverse Order with reverseOrder

Sorting ascending order means sorting with the small elements first then the big. This is usual ordering. Descending order means sorting with the big elements first then the small.

If you have a Comparator or Comparable of some kind, you can convert it into one that sorts into the reverse of the usual order. E.g. if the original sorts alphabetically, the new one will sort in reverse alphabetical order. Here is how you use it:

If you don't have a suitable base Comparator, just write an ordinary Comparator from scratch and reverse the operands to each compare inside it, or return - result instead of result.

Learning More

Sun’s Javadoc on the Comparable class : available:
Note java.lang.Comparable but java.util.Comparator.
Sun’s Javadoc on the Comparator class : available:
Sun’s Javadoc on Collections.reverseOrder() : available:
Sun’s Javadoc on Collections.reverseOrder(Comparator) : available:

CMP homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.59] The information on this page is for non-military use only.
You are visitor number 25,915. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/comparable.html J:\mindprod\jgloss\comparable.html