.NET 4.5 中 List<T>.Sort 的行为从 .NET 4.0 改变了吗? [英] Behaviour of List<T>.Sort in .NET 4.5 changed from .NET 4.0?

查看:15
本文介绍了.NET 4.5 中 List<T>.Sort 的行为从 .NET 4.0 改变了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个面向 .NET 4.0 的项目中进行了以下测试:

I have the following test inside a project targeting .NET 4.0:

[TestFixture]
public class Donkey
{
    [Test]
    public void TestListSorting()
    {
        var expected = new[]
                    {
                                MockRepository.GenerateStub<IComparable>(),
                                MockRepository.GenerateStub<IComparable>()
                    };

        var sorted = new List<IComparable>(expected);

        CollectionAssert.AreEqual(expected, sorted);
        sorted.Sort();
        CollectionAssert.AreEqual(expected, sorted);

    }
}

如果我在只安装了 .NET 4.0 的机器上运行它,它会失败.如果我在只安装了 .NET 4.5 的机器上运行它,它就会通过.

If I run it on a machine with only .NET 4.0 installed, it fails. If I run it on a machine with only .NET 4.5 installed, it passes.

我假设在 .NET 4.5 中,Sort 的实现已更改为在对对象列表进行排序时保持顺序,每个对象都从 CompareTo 返回 0.

I am assuming that in .NET 4.5 the implementation of Sort has been changed to maintain order when sorting a list of objects which each return 0 from CompareTo.

现在,把这个测试明显的疯狂放在一边.我知道依赖这种行为是很疯狂的.

当然这是一个重大变化?本页上没有列出关于 .NET 4.0 和 4.5 之间的兼容性.

Surely this is a breaking change? It is not listed on this page about compatibility between .NET 4.0 and 4.5.

这是有原因的吗?我错过了什么吗?是否有另一个页面显示实际的重大更改?我应该坐下来停止恐慌吗?

Is there a reason for this? Am I missing something? Is there another page which shows actual breaking changes? Should I just have a sit down and stop panicking?

推荐答案

我已经回答了一个与此类似的问题.排序方法在 4.5 和 4.0 之间发生了变化,从快速排序变为内省排序.

I've answered a similar question to this before. The sorting method has changed between 4.5 and 4.0, from a quick sort to an introspective sort.

它实际上更快,但仍然不是稳定的排序1,也就是说,通过保留相等项的顺序,每次执行都具有相同的输出.由于没有 List.Sort 的实现是一种稳定的排序,我认为您在上面运行单元测试的次数不够多,以至于在两个运行时都出现错误?

It's actually faster, but still it is not a stable sort1, that is, one that has the same output for every execution by preserving the order of equal items. Since no implementation of List.Sort is a stable sort, I don't think you've run your unit test above enough times to have it error in both runtimes?

我尝试使用等效代码和返回 0 的比较器自己重现它.有时会保留列表顺序,有时不会,在 .NET 4.5 和 .NET 3.5 中.

I tried to reproduce it myself with equivalent code and a comparer that returns 0. Sometimes the list order is preserved and sometimes it is not, in both .NET 4.5 and .NET 3.5.

即使它确实将排序类型从稳定更改为不稳定,这不是破坏性更改.使用的排序类型和确切的输出不是 List.Sort 合同的一部分.方法契约保证的只是您的项目将根据所使用的比较器进行排序.

Even if it did change the sort type from stable to unstable, its not a breaking change. The type of sort used and the exact output is not part of the contract for List.Sort. All that the method contract guarantees is that your items will be in sorted order according to the comparer used.

1 根据使用 QuickSortHeapSort 的混合定义,它应该是一个不稳定排序.甚至算法的设计者 David Musser 在他的论文中也指出:

1 By definition of using a mix of QuickSort and HeapSort, it should be an unstable sort. Even David Musser, the designer of the algorithm states in his paper:

与快速排序一样,Introsort 也不稳定——不保留等效元素的顺序——因此仍然需要对稳定的排序例程有单独的要求.

这篇关于.NET 4.5 中 List&lt;T&gt;.Sort 的行为从 .NET 4.0 改变了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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