排序双打的阵列与楠它

排序双打的阵列与楠它

本文介绍了排序双打的阵列与楠它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是更多的是你能解释一下这个类型的问题比它什么都重要。

我碰到在工作中的问题,我们是在一个表中使用NaN值,但是当表进行了排序,它出来了一个很奇怪的,奇怪的方式。我想楠摆弄一些东西,所以我写了一个测试应用程序,看看是否是真的。这是我做的。

 静态无效的主要(字串[] args)
{
    双[] someArray = {4.0,2.0,double.NaN,1.0,5.0,3.0,double.NaN,10.0,9.0,8.0};

    的foreach(在someArray双DB)
    {
        Console.WriteLine(DB);
    }

    的Array.Sort(someArray);
    Console.WriteLine(\ñ\ N);
    的foreach(在someArray双DB)
    {
        Console.WriteLine(DB);
    }

    到Console.ReadLine();
}
 

这给了结果:

  4,2,NaN,那么1,5,3,NaN的,10,9,8
 

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

所以,是的,在南有些怎样炼成的排序阵列以一种奇怪的方式进行排序。

要引用弗莱; 为什么这些东西?

解决方案

编辑(。总之最后的最后。):这个的的错误

请参阅错误报告错误的名单,其中,双/单> .Sort()[.NET35]列表中包含double.NaN ,然后去给汉斯帕桑特向上票在Why确实.NET 4.0排序这个数组不同于.NET 3.5?我从中撕开的链接。

历史沉思

[查看帖子:Why确实.NET 4.0排序这个数组不同于.NET 3.5?,在那里,希望更多这方面的有益讨论的特定问题可以计算出真实的。我已经交叉张贴这种反应有作为。]

行为指出.NET4菲尔是,在的CompareTo定义。请参见 double.CompareTo 以.NET4。这是相同的行为却.NET35和的是两个版本保持一致,每个方法文档...

的Array.Sort(双[])似乎并没有被使用的CompareTo(双[])如预期,这很可能是一个错误 - 下面的注释。我很想对以下澄清/更正。

在任何情况下,使用的答案> < == 解释为什么的的经营者不工作的,但无法解释为什么的Array.Sort 会导致不可预测的输出。下面是我的一些调查结果,因为微薄的,因为他们可能。

首先, double.CompareTo(T) 方法的文档 - 这个排序是明确定义根据文档

在LINQPad(3.5和4,都具有相同的结果):

  0d.CompareTo(0D)使用.dump(); // 0
double.NaN.CompareTo(0D)使用.dump(); // -1
double.NaN.CompareTo(double.NaN)使用.dump(); // 0
0d.CompareTo(double.NaN)使用.dump(); // 1
 

使用的CompareTo(对象)有同样的结果:

  0d.CompareTo((对象)0D)使用.dump(); // 0
double.NaN.CompareTo((对象)0D)使用.dump(); // -1
double.NaN.CompareTo((对象)double.NaN)使用.dump(); // 0
0d.CompareTo((对象)double.NaN)使用.dump(); // 1
 

所以这不是问题。

现在,从 的Array.Sort(对象[]) 文件 - 有没有用的制造> < == (根据文档) - 只要的CompareTo(对象)

同样, 的Array.Sort(T []) 使用的CompareTo(T)

让我们来看看:

LINQPad(4):

 变种AR =新的双[] {double.NaN,0,1,double.NaN};
的Array.Sort(AR);
ar.Dump();
//为NaN,NaN的,0,1
 

LINQPad(3.5):

 变种AR =新的双[] {double.NaN,0,1,double.NaN};
的Array.Sort(AR);
ar.Dump();
//为NaN,0,NaN,则1
 

LINQPad(3.5) - 注意数组对象是与行为是预期每的CompareTo 合同

 变种AR =新的对象[] {double.NaN,0D,1D,double.NaN};
的Array.Sort(AR);
ar.Dump();
//为NaN,NaN的,0,1
 

嗯。真。结论:

我不知道。

快乐编码。

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 when the table was sorted, it came out in a very strange, strange manner. I figured NaN was mucking up something so I wrote up a test application to see if this is true. This is what I did.

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();
}

Which gave the result:

Before:

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

After:

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

So yes, the NaN some how made the sorted array to be sorted in a strange way.

To quote Fry; "Why is those things?"

解决方案

Edit (conclusion. final. end.): This is a bug.

See bug-report Bug in List<double/single>.Sort() [.NET35] in list which contains double.NaN and go give Hans Passant an up-vote at the Why does .NET 4.0 sort this array differently than .NET 3.5? from which I ripped the link.

Historical musings

[See the post: Why does .NET 4.0 sort this array differently than .NET 3.5?, where, hopefully, more useful discussion on this particular issue can be figured out for real. I have cross-posted this response there as well.]

The behavior pointed out in .NET4 by Phil is that defined in CompareTo. See double.CompareTo for .NET4. This is the same behavior as in .NET35 however and should be consistent in both versions, per the method documentation...

Array.Sort(double[]): doesn't seem to be using CompareTo(double[]) as expected and this may very well be a bug -- note the difference in Array.Sort(object[]) and Array.Sort(double[]) below. I would love clarification/corrections on the following.

In any case, the answers using > and < and == explain why those operators don't work but fail to explain why Array.Sort leads to unexpected output. Here are some of my findings, as meager as they may be.

First, the double.CompareTo(T) method documentation -- this ordering is well-defined according to the documentation:

In LINQPad (3.5 and 4, both have 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 that's not the problem.

Now, from the Array.Sort(object[]) documentation -- there is no use of >, < or == (according to the documentation) -- just CompareTo(object).

Likewise, Array.Sort(T[]) uses CompareTo(T).

Let's see:

LINQPad (4):

var ar = new double[] {double.NaN, 0, 1, double.NaN};
Array.Sort(ar);
ar.Dump();
// NaN, NaN, 0, 1

LINQPad (3.5):

var ar = new double[] {double.NaN, 0, 1, double.NaN};
Array.Sort(ar);
ar.Dump();
// NaN, 0, NaN, 1

LINQPad (3.5) -- NOTE THE ARRAY IS OF OBJECT and the behavior is "expected" per the CompareTo contract.

var ar = new object[] {double.NaN, 0d, 1d, double.NaN};
Array.Sort(ar);
ar.Dump();
// NaN, NaN, 0, 1

Hmm. Really. In conclusion:

I HAVE NO IDEA.

Happy coding.

这篇关于排序双打的阵列与楠它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:39