Reputation: 3
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
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
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