为什么.NET 4.0与.NET 3.5不同的排序方式? [英] Why does .NET 4.0 sort this array differently than .NET 3.5?

查看:122
本文介绍了为什么.NET 4.0与.NET 3.5不同的排序方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个stackoverflow问题提出了一个有趣的问题,关于使用NaN值排序双精度数组。 OP发布了以下代码:

  static void Main(string [] args)
{
double [] someArray = {4.0,2.0,double.NaN,1.0,5.0,3.0,double.NaN,10.0,9.0,8.0};

foreach(someArray中的double db)
{
Console.WriteLine(db);
}

Array.Sort(someArray);
Console.WriteLine(\\\
\\\
);
foreach(someArray中的double db)
{
Console.WriteLine(db);
}

Console.ReadLine();
}

当您在.NET 3.5框架下运行时,数组按如下:



1,4,NaN,2,3,5,8,9,10,NaN / p>

当您在.NET 4.0下运行它时,数组在逻辑上稍微排序:



NaN,NaN,1,2,3,4,5,8,9,10



我可以明白为什么会排序在.NET 3.5中(因为NaN不等于,小于或大于任何东西)奇怪的是。我也可以理解为什么它会排序在.NET 4.0中的方式。我的问题是,为什么这个从3.5变为4.0? Microsoft文档在哪里修改?

解决方案

这是一个错误修复。具有错误详细信息的反馈报告在这里。 Microsoft对错误报告的回复:


请注意,此错误会影响以下内容:




  • Array.Sort(),其中数组包含Double.NaN

  • Array.Sort(),其中数组包含Single.NaN

  • 任何上述的呼叫者,例如List.Sort(),其中list包含Double.NaN



这个错误将在运行时的下一个主要版本中被修复;直到那时,您可以通过使用执行正确排序的自定义IComparer来解决此问题。如解决方法注释中所述,不要使用Comparer.Default,因为这是特殊的,具有不能正确处理NaN的快捷方式排序例程。相反,您可以提供自己的比较器,提供同等的比较,但不会是特殊的。



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

static void Main(string[] args)
{
    double[] someArray = { 4.0, 2.0, double.NaN, 1.0, 5.0, 3.0, double.NaN, 10.0, 9.0, 8.0 };

    foreach (double db in someArray)
    {
        Console.WriteLine(db);
    }

    Array.Sort(someArray);
    Console.WriteLine("\n\n");
    foreach (double db in someArray)
    {
        Console.WriteLine(db);
    }

    Console.ReadLine();
}

When you run this under the .NET 3.5 framework, the array is sorted as follows:

1,4,NaN,2,3,5,8,9,10,NaN

When you run it under .NET 4.0, the array is sorted somewhat more logically:

NaN,NaN,1,2,3,4,5,8,9,10

I can understand why it would sort weirdly in .NET 3.5 (because NaN is not equal to, less than, or greater than, anything). I can also understand why it would sort the way it does in .NET 4.0. My question is, why did this change from 3.5 to 4.0? And where is the Microsoft documentation for this change?

解决方案

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.

这篇关于为什么.NET 4.0与.NET 3.5不同的排序方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆