Edit (end. End.): This is a mistake.
See error report Error in list <double / single> .Sort () [.NET35] in the list that contains double.NaN and go, give Hans Passan a vote on Why .NET 4.0 sorts this array differently than. NET 3.5? from which I broke the link.
Historical reflections
[Cm. message: Why does .NET 4.0 sort this array differently than .NET 3.5? where, hopefully, a more useful discussion of this particular problem can be thought up for real. I also posted this answer here.]
The behavior specified in .NET4 from Phil is specified in CompareTo. See double.CompareTo for .NET4. This is the same behavior as in .NET35, but should be consistent in both versions, according to the method documentation ...
Array.Sort(double[]) : doesn't seem to use CompareTo(double[]) as expected, 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:
In any case, answers using > and < and == explain why these statements do not work, but cannot explain why Array.Sort leads to unexpected output. Here are some of my finds that are as scarce as they can be.
First, 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. MASS OF THE 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 no IDEA.
Happy coding.