Reputation: 47
The program sorts the array in ascending order but on swapping id
and compareid
in the return statement the array sorts in descending order but it has no effect on the output of System.out.println(e[1].compareTo(e[0]));
it returns 1
in both cases. Why is it so?
package example;
import java.util.Arrays;
class Example implements Comparable<Example> {
int id;
public int compareTo(Example ob) {
int compareid = ob.id;
return Integer.compare(id, compareid); // problem
}
}
class comparableinterface {
public static void main(String args[]) {
Example e[] = new Example[3];
e[0] = new Example();
e[0].id = 2;
e[1] = new Example();
e[1].id = 3;
e[2] = new Example();
e[2].id = 0;
Arrays.sort(e);
for (Example temp : e) {
System.out.println(temp.id);
}
System.out.println(e[1].compareTo(e[0]));
}
}
Upvotes: 1
Views: 677
Reputation: 1286
You should call the compareTo() method on an instance of an Integer, not the static compare()
Try this:
int id;
public int compareTo(Example ob) {
Integer compareid = ob.id;
return compareid.compareTo(id);
}
Upvotes: 0
Reputation: 39447
Because in order to sort your array ascending/descending you also change your compareTo
method in example
to compare for < respectively > (i.e. you swap the logic in the compareTo method). That's why it gives you the same result from System.out.println(e[1].compareTo(e[0]));
. Basically after the change, your compareTo
does not check for "is smaller" any more but checks for "is bigger". So even though System.out.println(e[1].compareTo(e[0]));
returns 1 in both cases, in the first case it tells you "e[1] is bigger than e[0]" and in the second case it tells you "e[1] is smaller than e[0]". It's kind of tricky, think about it.
Upvotes: 0
Reputation: 1286
compareTo method is referred to as its natural comparison method.
The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C.
See also the java.utils.Arrays.mergeSort() source code to see how compareTo is used to sort an array:
for (int j=i; j>low &&
((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
swap(dest, j, j-1);
Upvotes: 0
Reputation: 4525
You are using Integer.compare(id,compareid)
in your overrided compareTo(Example ob)
method, and for your information,
Integer.compare(id,compareid) returns the value 0 if id == compareid; a value less than 0 if id < compareid; and a value greater than 0 if id > compareid.
And you are calling compareTo(Example ob)
after sorting the array, this is why the method always returning 1
.
Try calling compareTo(Example ob)
before sorting the array.
Upvotes: 0
Reputation: 31290
The result of compareTo reflects a certain order between the object and the argument. This will always be +1 between the first and second in a sorted array, sorted according to whatever is expressed by compareTo.
It is not an indication of the numeric relation!
Upvotes: 1
Reputation: 11917
Because your comparison is being performed after you have sorted the array, and Arrays.sort(e) changes the contents of the array e.
Move
System.out.println(e[1].compareTo(e[0]));
to before the sort, and it will behave as you expected.
Upvotes: 3