I have like this class to compare Y Cordinates (descending order) and I have predefined Point class .
In main class ,I am sending Compare Y array which is keeping points(type =Point) Arrays.sort(array); and it gives me ClassCastExeption ,
how can ı fix this problem.
public class CompareY implements Comparator<Point> {
public CompareY(){
}
@Override
public int compare(Point a1, Point a2) {
if (a1.y > a2.y)
return -1;
else if (a1.y < a2.y)
return 1;
else {
if (a1.x < a2.x)
return 1;
else if (a1.x > a2.x)
return -1;
else
return 0;
}
}
Objectarray that contains objects of different types.xandyare int, you can usereturn a1.y - a2.yto avoid verbosity, if not, thenMath.signum()will help.ClassCastExceptionand how to fix this. I think the question that you have posted is complete and no additional code needs to be posted from your end to narrow down the issue. +1