I need to sort an array that contains null values , The null values represent invalid data that i have set to null but cannot be simply removed from the array as they represent an invalid piece of data The null values must be kept in place i.e sort all other values except the null values The error that is thrown is a NullPointerException on the call to Arrays.sort();
public static double getMedian(Double[] values) {
Double[] copy = Arrays.copyOf(values, values.length);
Arrays.sort(copy);
double median;
if (copy.length % 2 == 0)
median = (copy[copy.length / 2] + copy[copy.length / 2 - 1]) / 2;
else
median = copy[copy.length / 2];
return median;
}
All help and/or suggestions are greatly appreciated.
Comparator<Double>and supply it toArrays.sort()if (o1 == null) { return -1; } else if (o2 == null) { return +1; }, etc.