对其中包含 NaN 的 Double 数组进行排序
这更像是一个“你能解释一下这个”类型的问题吗?
我在工作中遇到一个问题,我们在表中使用 NaN 值,但是当对表进行排序时,它以一种非常奇怪的方式出现。我认为 NaN 搞砸了一些事情,所以我编写了一个测试应用程序来看看这是否属实。这就是我所做的。
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();
}
结果如下:
之前:
4,2,NaN,1,5,3,NaN,10,9,8
之后:
1,4,NaN,2,3,5,8,9,10,NaN
所以是的,NaN 是如何使排序数组以一种奇怪的方式排序的。
引用弗莱的话; “为什么会有那些东西?”
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?"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相信这是因为
对它们的比较失败了,这导致了整个排序的混乱。
I believe that's because
so the comparison on them breaks down, and that throws off the entire sort.
编辑(结论。最终。结束。):这是一个错误。
请参阅错误报告 列表中的错误.Sort() [.NET35] 包含 double.NaN 的列表,并在 为什么 .NET 4.0 对此数组进行排序与 .NET 3.5 不同? 我从中删除了链接。
历史沉思
[参见帖子:为什么 .NET 4.0 对该数组的排序方式与 .NET 3.5 不同?,希望能够真正找到关于这个特定问题的更有用的讨论。我也在那里交叉发布了这个回复。]
Phil 在 .NET4 中指出的行为是在 CompareTo 中定义的。请参阅 .NET4 的 double.CompareTo。这与 .NET35 中的行为相同,但是根据方法文档,两个版本中的行为应该保持一致...
Array.Sort(double[])
: >似乎没有按预期使用CompareTo(double[])
,这很可能是一个错误 - 请注意 Array.Sort(object[]) 和 Array.Sort( double[]) 下面。我希望对以下内容进行澄清/更正。无论如何,使用
>
和<
和==
的答案解释了为什么这些运算符不起作用 但无法解释为什么Array.Sort
会导致意外的输出。以下是我的一些发现,尽管它们可能微不足道。首先,
double.CompareTo(T)
方法文档 - 根据文档明确定义了此顺序:在 LINQPad(3.5 和 4,两者都有相同的结果)中:
使用
CompareTo(object)
具有相同的结果:所以这不是问题。
现在,从
Array.Sort (object[])
文档 -- 没有使用>
、<
或==< /code> (根据文档)
- 只是
CompareTo(object)
。同样,
Array.Sort(T [])
使用CompareTo(T)
。让我们看看:
LINQPad (4):
LINQPad (3.5):
LINQPad (3.5) -- 注意数组是对象,并且行为是根据
CompareTo
合约“预期”的。唔。真的。总之:
我没有任何想法。
快乐编码。
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 usingCompareTo(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 whyArray.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):
Using
CompareTo(object)
has the same results:So that's not the problem.
Now, from the
Array.Sort(object[])
documentation -- there is no use of>
,<
or==
(according to the documentation) -- justCompareTo(object)
.Likewise,
Array.Sort(T[])
usesCompareTo(T)
.Let's see:
LINQPad (4):
LINQPad (3.5):
LINQPad (3.5) -- NOTE THE ARRAY IS OF OBJECT and the behavior is "expected" per the
CompareTo
contract.Hmm. Really. In conclusion:
I HAVE NO IDEA.
Happy coding.
从概念上讲,NaN 不是数字,因此与数字进行比较是没有意义的,因此:
要解决这个问题,您需要编写自己的比较器,使用 IsNaN 使 NaN 小于(或大于)所有数字,以便它们全部出现在这一类的一端。
编辑:这是比较版本的示例:
Conceptually NaN is Not a Number, so comparing to a number makes no sense, hence:
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:
实际上,奇怪的排序行为是 .NET 3.5 中的错误造成的。该错误已通过 .NET 4.0 得到解决。
解决此问题的唯一方法是使用您自己的自定义比较器,或升级到 .NET 4.0。请参阅为什么 .NET 4.0 对此数组的排序方式与 .NET 3.5 不同?
Actually, the strange sorting behavior is the result of a bug in .NET 3.5. The bug was addressed with .NET 4.0.
The only way to resolve it is to use your own custom comparer, or upgrade to .NET 4.0. See Why does .NET 4.0 sort this array differently than .NET 3.5?
因为您使用的是默认排序,即快速排序算法;该实现执行不稳定的排序;也就是说,如果两个元素相等,它们的顺序可能不会保留
since you are using the default sort which is QuickSort algorithm; the implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved