2

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;
    }

}
9
  • Can you point out where you get an error..? Commented May 9, 2015 at 12:14
  • and show us how you use your construct. btw you can achieve line breacks in questions by adding to whitespaces at the end of each line Commented May 9, 2015 at 12:15
  • 1
    Can you show the code where you get the error. Looks like you are trying to sort an Object array that contains objects of different types. Commented May 9, 2015 at 12:35
  • If x and y are int, you can use return a1.y - a2.y to avoid verbosity, if not, then Math.signum() will help. Commented May 9, 2015 at 12:45
  • @serdar See my answer for an explanation on why you get a ClassCastException and 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 Commented May 9, 2015 at 12:53

1 Answer 1

3

The Arrays.sort(Object[] a) method expects a type that implements Comparable. If you are creating an array of CompareY objects, you should note that CompareY does not implement Comparabe and therefore, you get a ClassCastException at runtime since CompareY cannot be cast to a Comparable. Similarly, if you are creating an array of Point objects, you will still get a ClassCastException since Points does not implement Comparable

To fix the issue :

  1. Create an array of Point objects.
  2. Use the Arrays.sort method that takes a Comparator.

Example :

Point[] points = new Point[]{new Point(..),new Point(...)};
Arrays.sort(points,new CompareY());

Alternately, you can have the Point class implement Comparable and define the natural ordering for Points. You can then use the Array.sort(Object[] a) method directly.

Sign up to request clarification or add additional context in comments.

5 Comments

ı already implement comparable interface in point class and it is work properly.ı used Array.sort(points,new CompareY()); it works .thank you for answer.
@serdarözer I am posting this as part of the question. Are you still getting a ClassCastException after implementing Comparable? Can you edit your question with the code that creates the array and passes it to the Arrays.sort method please?
@serdarözer So you are saying that you are facing the issue even after implementing Comparable? I don't think that's possible. That being said, I believe my answer pointed (pun intended) you in the right direction so don't forget to upvote and accept the answer :)
no just write after this piece of code Array.sort(points,new CompareY()); and ı accepted answer but ı dont have enough reputation for upvote :-)
@serdarözer My bad. I keep forgetting the restriction on upvotes until a certain rep is reached ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.