Why does .NET 4.0 sort this array differently than .NET 3.5?

It’s a bug fix. The feedback report with the bug details is here. Microsoft’s response to the bug report:

Note that this bug affects the following:

  • Array.Sort(), where the array contains Double.NaN
  • Array.Sort(), where the array contains Single.NaN
  • any callers of above, for example on List.Sort(), where list contains Double.NaN

This bug will be fixed in the next major version of the runtime; until then you can work around this by using a custom IComparer that does the correct sorting. As mentioned in the workaround comments, don’t use Comparer.Default, because this is special-cased with a shortcut sort routine that doesn’t handle NaN correctly. Instead, you can provide your own comparer that provides an equivalent comparision, but won’t be special-cased.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)