Comparison in Java
Java does not allow a function to be passed as a function argument,
and its various types must be compared in different ways:
- The primitive types int, long, float and
double are compared using < and >. They cannot
use .compareTo() .
- String uses .compareTo(); it cannot use < and >.
- An application object type can be given a .compareTo() method,
but that allows only one way of sorting.
- A Comparator can be passed as a function argument, and this
allows a custom comparison method.
public static void
mySort( AnyType[] a,
Comparator<? super AnyType> cmp) {...}
class MyOrder implements Comparator<MyObject>
{
public int compare(MyObject x, MyObject y)
{ return ( x.property() - y.property() ); }}
Contents
Page-10
Prev
Next
Page+10
Index