java - Sorting an array in ascending order that contains null values -


i need sort array contains null values , null values represent invalid data have set null cannot removed array represent invalid piece of data null values must kept in place i.e sort other values except null values error thrown nullpointerexception on call 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 and/or suggestions appreciated.

add comparator , return appropriate sign, indicate less than, equal or greater than. example:

class mycomparator<double> implements comparator {     // change value -1 inverse sort direction.     var direction = 1;      public int compare(double o1, double o2) {         int sign = 0;         if (o1 == null) {            sign = -1;         } else if (o2 == null) {            sign = +1;         } else {            sign = o1.compareto(o2);         }                return sign * direction;     }  }  arrays.sort(copy, new mycomparator()); 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -