T>的名单,其中行为.Sort在.NET 4.5的.NET 4.0改变了吗?名单、行为、改变了、GT

2023-09-03 11:53:28 作者:伤丶半世流离

我有一个项目中下面的测试针对.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 的实施排序已被更改,以保持排序对象的列表时,为了每个符 0 的CompareTo

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?

推荐答案

我已经回答了一个similar问题之前来此。该分类方法之间的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 ,就是一个具有相同输出的每个执行由preserving等于项目的顺序。由于没有实施 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?

我试图复制它自己​​相等于code和比较器,返回0。有时列表顺序是preserved,有时它不是,在这两个.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 通过使用快速排序的组合定义堆排序,它应该是一个不稳定的排序。即便是大卫·马瑟,在他的论文设计师:

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,如快速排序,是不是稳定的 - 不preserve相当于元素的顺序 - 所以仍然需要有一个稳定的排序例程一个单独的要求。

 
精彩推荐
图片推荐