Skip to content
Advertisement

Can’t pass wildcard Comparator to function

I have the following sort method in a clone of ArrayList:

JavaScript

The sorter object is an instance of Sort:

JavaScript

I get the following error on the sorter.mergeSort line:

JavaScript

I’m not sure why this is happening as both the parameter and argument have type Comparator<? super E>.

Advertisement

Answer

Thanks to everyone for their comments. I’ve now fixed it.

JavaScript

As Kayaman said, I shouldn’t use rawtypes so I’ve given ascComparator parameter T. I’ve also casted array to E instead of Comparable as it didn’t do what I thought it did. I’ve also changed Sort:

JavaScript

As pointed out by Louis Wasserman I don’t need the Comparable type cast.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement