The interface to add the capability of comparing to another value.
While this interface can be called by a user directly, its main intention is to allow certain system types, in particular, a container structure that can maintain an order among its elements, to sort the stored elements according to the result returned by this interface.
The implementation doesn't have to cover all the possible types. Instead, it may only handle those types that would appear in the data structure in question. For example, if a user only puts objects of certain classes into a list which she is intent to sort later, she can knowingly implement this interface such that it only checks, casts, and compares to these classes.
To tolerate incompatible values, one may simply return 0 if the other's type is not in the scope. The calling class then treats the two as if they were equal. This approach, however, is not going to guarantee even a partially correct order, since the underlying sorting algorithm may very likely be based on the assumption that all the values are comparable. If tolerance is less important than correctness, throwing an exception is another option.
Type | Name | Signature |
---|---|---|
method | compare | public int compare(var) |
public int compare(var another)
Compare this object to another.
Parameters
Returns