I have written the following code:
public class NewClass2 implements Comparator<Point>
{
public int compare(Point p1, Point p2)
{
return (int)(p1.getY() - p2.getY());
}
}
If I let’s say have two double numbers, 3.2 - 3.1, the difference should be 0.1. When I cast the number to an int, however, the difference ends up as 0, which is not correct.
I therefore need compare() to return a double, not an int. The problem is, my getX field is a double. How can I solve this problem?
You don’t need to return
double.The
Comparatorinterface is used to establish an ordering for the elements being compared. Having fields that usedoubleis irrelevant to this ordering.Your code is fine.Sorry, I was wrong, reading the question again, this is what you need: