This is more of a \'Can you explain this\' type of question than it is anything else.
I came across a problem at work where we were using NaN values in a table, but
Conceptually NaN is Not a Number, so comparing to a number makes no sense, hence:
a < NaN = false for all a,
a > NaN = false for all a and
NaN != NaN (!)
To solve this you need to write your own comparer that uses IsNaN to make NaNs smaller than (or larger than) all numbers, so that they will all appear at one end of the sort.
Edit: Here's a sample of the Comparison version:
class Program
{
private static int NaNComparison(double first, double second)
{
if (double.IsNaN(first))
{
if (double.IsNaN(second)) // Throws an argument exception if we don't handle both being NaN
return 0;
else
return -1;
}
if (double.IsNaN(second))
return 1;
if (first == second)
return 0;
return first < second ? -1 : 1;
}
static void Main(string[] args)
{
var doubles = new[] { double.NaN, 2.0, 3.0, 1.0, double.NaN };
Array.Sort(doubles, NaNComparison);
}
}