- negative value if a should come before b
- 0 if a and b are the same
- positive value otherwise
Programmers are responsible for implementing the
compareTo method whenever they create a class whose objects might need to be ordered. The class of the method’s formal parameter should be the same as that of the class being created.
* Compares this object with the specified object for order. Returns a negative
* integer, zero, or a positive integer as this object is less than, equal to, or
* greater than the specified object.
* @param otherObject the object whose order is compared to this object’s
* @return a negative integer, zero, or a positive integer as this object is less
* than, equal to, or greater than the specified object
public int compareTo(Object otherObject)
// actual comparison goes here
} // end of method compareTo(Object otherObject)
The class being created should implement the
Comparable interface; the sole method of that interface is
[This page last updated 2020-12-23 at 12h13 Toronto local time.]
© 2007–2021 Hersch, Bear & Company Limited. All rights reserved. “Grammar Authority,” “grammarauthority.com,” “touque.ca,” and “Canada’s Thinking Cap” are trademarks of Hersch, Bear & Company Limited. All other trademarks and copyrights are the property of their respective owners.