1

Here is my class:

public static class __9_7_Person implements Comparator<__9_7_Person> {
    private int height;
    private int weight;
    public __9_7_Person(int height, int weight) {
        this.height = height;
        this.weight = weight;
    }
    public int compare(__9_7_Person p1, __9_7_Person p2) {
        if (p1.height != p2.height) {
            return p1.height - p2.height;
        }
        else {
            return p1.weight - p2.weight;
        }
    }
}

I then created an array like this:

__9_7_Person p[] = {new __9_7_Person(60, 100),
                    new __9_7_Person(70, 150),
                    new __9_7_Person(56, 90),
                    new __9_7_Person(75, 190),
                    new __9_7_Person(60, 95),
                    new __9_7_Person(68, 110),
};

But got exception when I called Arrays.sort(p): "Exception in thread "main" java.lang.ClassCastException: ch_9$__9_7_Person cannot be cast to java.lang.Comparable"

1
  • 4
    I hope these aren't the actual names of your classes, with the underscores and all... Commented Jul 8, 2012 at 18:45

3 Answers 3

8

You implemented Comparator, not Comparable. You should implement Comparable instead of Comparator.

As stated in the docs:

All elements in the array must implement the Comparable interface.

Comparators are useful if you want to be able to have different sorting order for the same class. In this case, Comparable can't be used

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

1 Comment

+1, added the quote from Arrays.sort that specifies that requirement.
4

You should implement Comparable for a natural ordering, in which case you don't need to pass a separator comparator into Arrays.sort. Or you could implement Comparator<__9_7_Person> (probably in a separate class, e.g. HeightWeightPersonComparator) and call:

Arrays.sort(p, new HeightWeightPersonComparator());

It's important to understand the difference between Comparable and Comparator. A Comparable implementation says "I know how to compare myself to another object of an appropriate type" - where a Comparator implementation says "I know how to compare two objects of appropriate types".

Obviously any type can only implement Comparable once (within reason), whereas there can be any number of Comparator implementations. Using a separate Comparator is more flexible unless there's an "obvious" comparison you should use. If you don't specify a Comparator, Arrays.sort will assume that each of the elements in the array can compare itself with other elements of the array, i.e. they implement Comparable.

Comments

0

You need to implement the java.lang.Comparable interface, not the java.util.Comparator interface.

Comments

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.