[This is my answer, shameless, torn from another post. It would be nice if someone double.CompareTo this further - double.CompareTo and double.CompareTo(double) clearly defined as below, so I suspect there is some kind of Array.Sort magic for a certain type.]
Array.Sort(double[]) : it seems that CompareTo(double[]) is not used, and this can be a very mistake - note the difference in Array.Sort (object []) and Array.Sort (double []) below. I would like to clarify / fix the following:
Firstly, double.CompareTo(T) documentation - this ordering is clearly defined in accordance with the documentation :
Less than zero: This instance is less than the value. -or- This instance is not a number (NaN), but the value is a number.
Zero This instance is equal to the value. -or- And this instance and value are not a number (NaN), PositiveInfinity or NegativeInfinity.
Greater than zero: This instance is greater than the value. -or- This instance is a number and a value, not a number (NaN).
In LINQPad (3.5 and 4 both have the same results):
0d.CompareTo(0d).Dump(); // 0 double.NaN.CompareTo(0d).Dump(); // -1 double.NaN.CompareTo(double.NaN).Dump(); // 0 0d.CompareTo(double.NaN).Dump(); // 1
Using CompareTo(object) has the same results:
0d.CompareTo((object)0d).Dump(); // 0 double.NaN.CompareTo((object)0d).Dump(); // -1 double.NaN.CompareTo((object)double.NaN).Dump(); // 0 0d.CompareTo((object)double.NaN).Dump(); // 1
So no problem.
Now from Array.Sort(object[]) documentation is not used > , < or == (according to the documentation) is just CompareTo(object) .
Sorts the elements in the entire one-dimensional array using the IComparable implementation of each element of the array.
Similarly, Array.Sort(T[]) uses CompareTo(T) .
Sorts the elements in the entire array using the implementation of the universal IComparable (Of T) interface of each element of the array.
We'll see:
LINQPad (4):
var ar = new double[] {double.NaN, 0, 1, double.NaN}; Array.Sort(ar); ar.Dump();
LINQPad (3.5):
var ar = new double[] {double.NaN, 0, 1, double.NaN}; Array.Sort(ar); ar.Dump();
LINQPad (3.5) - NOTE. ARRAY IS OBJECT , and the behavior is "expected" in the CompareTo contract.
var ar = new object[] {double.NaN, 0d, 1d, double.NaN}; Array.Sort(ar); ar.Dump();
Hm. Indeed. Finally:
I HAVE NOT AN IDEA - but I suspect that there is some "optimization", as a result of which CompareTo(double) not called.
Happy coding.