The
compareTo() method in
org.javatuples is used to compare the order of a Tuple object with the object passed as the parameter. This method can be used for any tuple class object of javatuples library. It returns the difference in the ASCII value of the 1st different element present in the passed object, from the called object.
Syntax:
tupleObject1.compareTo(tupleObject2)
Parameters: This method takes a mandatory parameter
tupleObject2 which is the JavaTuple object to be compared.
Return Value: This method the difference in the ascii value of the 1st different element present in the passed object, from the called object. It might be negative, 0, or positive integer value.
Below programs illustrate the various ways to use compareTo() method:
Program 1: When compareTo() gives negative result:
Output:
-25
Explanation:
The ascii value of 'a' is 97 while that of 'z' is 122. Hence a.compareTo(z) yielded -25 (=122-97) as the result.
Program 1: When compareTo() gives 0 as result:
Output:
0
Explanation:
Since "Geeks" is same as "Geeks". Hence they are equal. So in this case Geeks.compareTo(Geeks) yielded 0.
Program 3: When compareTo() gives positive result: