Here's my code:
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
public class SortingUsingGenerics {
public static void main(String[] args) {
Integer[] intArray = {1,5,4,2,10};
String[] stringArray = {"Ä","X","C","B","M"};
Float[] floatArray = {1.5f,2.8f,0.5f,10.3f,9.5f};
// Sort and Print
printArray(sortArray(intArray));
printArray(sortArray(stringArray));
printArray(sortArray(floatArray));
}
private static <MyArray> void printArray(MyArray[] inputArray) {
for (MyArray element : inputArray) {
System.out.print(element);
}
System.out.println();
}
public static <E> E[] sortArray(E[] inputArray){
if (inputArray instanceof Integer[]){
Collections.sort(Arrays.asList(inputArray),new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
if (o1 == o2) {
return 0;
} else if (o1 > 02) {
return 1;
} else {
return -1;
}
}
});
}
return inputArray;
}
}
The error I am getting is:
The method
sort(List<T>, Comparator<? super T>)in the type Collections is not applicable for the arguments(List<E>, new Comparator<Integer>(){})
Could you please explain what I'm doing wrong here?
else if (o1 > 02)is a bug. (02is an octal literal for the number2.)