Comparer 使 Sort 何时会抛出 ArgumentException?
Sort 的文档说 Sort 会抛出如果“比较器的实现在排序期间导致错误。例如,比较器在将项目与其自身进行比较时可能不会返回 0”,则抛出 ArgumentException。
除了给出的例子之外,谁能告诉我什么时候会发生这种情况?
The documentation for Sort says that Sort will throw an ArgumentException if "The implementation of comparer caused an error during the sort. For example, comparer might not return 0 when comparing an item with itself."
Apart from the example given, can anyone tell me when this would otherwise happen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
排序算法 (QuickSort) 依赖于可预测的 IComparer 实现。 在 BCL 中经过几十层间接之后,您最终会得到这个方法:
进一步了解 QuickSort 实现,您会看到如下代码:
基本上,如果 IComparer 错误地调用 Quicksort 并抛出 IndexOutOfRangeException,该异常被包装在 n 中参数异常。
这是坏 IComparer 的另一个例子
所以我想,简短的答案是,任何时候您的 IComparer 实现不能一致地比较文档中定义的值:
The sort algorithm (QuickSort) relies on a predictable IComparer implementation. After a few dozen layers of indirection in the BCL you end up at this method:
Going a bit further into the QuickSort implementation, you see code like this:
Basically if the IComparer misbehaves the Quicksort call with throw an IndexOutOfRangeException, which is wrapped in n ArgumentException.
Here is another example of bad IComparer's
So I guess, the short answer is, anytime your IComparer implementation does not consistently compare values as defined in the documentation:
我今天遇到了这个问题,经过调查,我发现有时调用我的比较器时 x 和 y 是对同一个对象的引用,并且我的比较器没有返回 0。修复后,我不再收到异常。
HTH,
埃里克
I ran into this today, and after investigating, I found that sometimes my comparer was being called with x and y being references to the same object, and my comparer was not returning 0. Once I fixed that, I stopped getting the exception.
HTH,
Eric