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

后端 未结 4 1725
感动是毒
感动是毒 2020-12-28 12:11

This stackoverflow question raised an interesting question about sorting double arrays with NaN values. The OP posted the following code:

static void Main(st         


        
4条回答
  •  囚心锁ツ
    2020-12-28 12:52

    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.

提交回复
热议问题