This is not rocket science ...
Paste this in place of the comment code:
if (a == null) { return b == null ? 0 : -1; } else if (b == null) { return 1; } else
This treats null as a smaller value than any non-zero Double instance.
Your version is incorrect:
if ((a==null) || (b==null)) {return -1;}
This says: "if a is null or b is null, then a is less than b."
This leads to fictitious relationships like
null < 1.0 AND 1.0 < null null < null
This thing causes tree invariants to collapse when zeros are displayed in the / set, and leads to inconsistent and unstable ordering of the keys ... and worse.
Requirements for a valid compare method are given in javadocs . The mathematical version is that the method should determine the general order in the domain of all possible input values.
source share