Interface used by types that have an intrinsic ordering.
The compareTo operation defines a total ordering of objects, which can be used for ordering and sorting.
The Comparable interface should be used for the natural ordering of a type. If a type can be ordered in more than one way, and none of them is the obvious natural ordering, then it might be better not to use the Comparable interface, and to provide separate Comparators instead.
It is recommended that the order of a Comparable agrees
with its operator == equality (
a.compareTo(b) == 0 iff
a == b),
but this is not a requirement.
For example, double and DateTime have
that do not agree with operator ==.
For doubles the compareTo method is more precise than the equality,
and for DateTime it is less precise.
(0.0).compareTo(-0.0); // => 1 0.0 == -0.0; // => true var dt = new DateTime.now(); var dt2 = dt.toUtc(); dt == dt2; // => false dt.compareTo(dt2); // => 0
The Comparable interface does not imply the existence
of the comparison operators
These should only be defined
if the ordering is a less-than/greater-than ordering,
that is, an ordering where you would naturally
use the words "less than" about the order of two elements.
If the equality operator and compareTo disagree,
the comparison operators should follow the equality operator,
and will likely also disagree with compareTo.
Otherwise they should match the compareTo method,
a < b iff
a.compareTo(b) < 0.
The double class defines comparison operators
that are compatible with equality.
The operators differ from
double.compareTo on -0.0 and NaN.
The DateTime class has no comparison operators, instead it has the more precisely named DateTime.isBefore and DateTime.isAfter.
Comparable a, Comparable b) → int
A Comparator that compares one comparable to another.
- hashCode → int
The hash code for this object.read-only, inherited
- runtimeType → Type
A representation of the runtime type of the object.read-only, inherited
other) → bool
The equality operator.inherited
T other) → int
Compares this object to another Comparable
Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.inherited
) → String
Returns a string representation of this object.inherited