Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Comparable

public interface Comparable

java.lang.Comparable<T>
Known Indirect Subclasses


This interface should be implemented by all classes that wish to define a natural order of their instances. sort(List) and java.util.Arrays#sort can then be used to automatically sort lists of classes that implement this interface.

The order rule must be both transitive (if x.compareTo(y) < 0 and y.compareTo(z) < 0, then x.compareTo(z) < 0 must hold) and invertible (the sign of the result of x.compareTo(y) must be equal to the negation of the sign of the result of y.compareTo(x) for all combinations of x and y).

In addition, it is recommended (but not required) that if and only if the result of x.compareTo(y) is zero, then the result of x.equals(y) should be true.

Summary

Public methods

abstract int compareTo(T another)

Compares this object to the specified object to determine their relative order.

Public methods

compareTo

Added in API level 1
int compareTo (T another)

Compares this object to the specified object to determine their relative order.

Parameters
another T: the object to compare to this instance.
Returns
int a negative integer if this instance is less than another; a positive integer if this instance is greater than another; 0 if this instance has the same order as another.
Throws
ClassCastException if another cannot be converted into something comparable to this instance.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.