user3443890
user3443890

Reputation: 3

How do I fix this error ? "the operator is undefined for the argument type(s) entry, entry"

I have just created an insertion method to sort the array, this is the code that I have done in that method;

public static void insertionSort (Entry[] array2){
    for (int i =0; i < array2.length; i++){
        Entry values = array2[i];
        int j = i-1;
        while (j >=0 && array2[j] > values){
            array2[j+1] = array2[j];
            j = j-1;
        }
        array2[j+1] = values;
    }
}

Upvotes: 0

Views: 839

Answers (2)

Louis Wasserman
Louis Wasserman

Reputation: 198163

If your Entry class implements Comparable, then you should replace

array2[j] > values

with

array2[j].compareTo(values) > 0

...because Java doesn't allow operator overloading, so you have to just call the compareTo method.

Upvotes: 0

robertoia
robertoia

Reputation: 2361

The problem is here:

while (j >=0 && array2[j] > values){

You are comparing two values of type Entry with '>', they can't be compared like that.

If you are using Map.Entry, it doesn't implement Comparable, so even compareTo would fail. Think about what you want to compare, keys or values?

Upvotes: 2

Related Questions