简体   繁体   English

List.Sort(按长度)在不同的计算机上返回不同的结果

[英]List.Sort(by length) returns different results on different computers

I have the following code : 我有以下代码:

List<string> Words = item.Split(' ').ToList<string>();
Words.Sort((a, b) => b.Length.CompareTo(a.Length));

Which is supposed to sort a List of words from a line in a file (item) according to their size. 应该根据文件(项目)的大小对文件(项目)中的单词列表进行排序。 However, if two words have the same length, they are supposed to be sorted by the order of appearence in the line. 但是,如果两个单词具有相同的长度,则应该按行中的外观顺序对它们进行排序。

The problem here is that if the line is, for example "abc", on my computer, the list will have three sorted items (0 - a, 1 - b, 2 - c), but on another computer, using the same .Net version (4.5), the sorted items will be (0 - c, 1 - b, 2 - a) 这里的问题是,如果该行是,例如“abc”,在我的计算机上,该列表将有三个已排序的项目(0 - a,1 - b,2 - c),但在另一台计算机上,使用相同的。净版(4.5),排序的项目将是(0 - c,1 - b,2 - a)

Is there a way to enforce the same result troughout different computers ? 有没有办法在不同的计算机上强制执行相同的结果?

List.Sort is an unstable sort , meaning in your case that elements of the same length can go in different order. List.Sort是一种不稳定的排序 ,在你的情况下,相同长度的元素可以按不同的顺序排列。

This implementation performs an unstable sort; 此实现执行不稳定的排序; that is, if two elements are equal, their order might not be preserved. 也就是说,如果两个元素相等,则可能不会保留它们的顺序。 In contrast, a stable sort preserves the order of elements that are equal. 相反,稳定的排序保留了相等元素的顺序。

You can force the same results using LINQ and set of OrderBy / ThenBy calls, instead of Sort . 您可以使用LINQ和OrderBy / ThenBy调用来强制执行相同的结果,而不是Sort

var result = source.Select((v, i) => new { v, i })
                   .OrderBy(x => x.v.Length)
                   .ThenBy(x => x.i)
                   .Select(x => x.v)
                   .ToList();

But you should be aware, that it will create new list, instead of sorting existing one in place: 但是你应该知道,它会创建新的列表,而不是对现有的列表进行排序:

The method List.Sort() is an unstable sort. List.Sort()方法是一种不稳定的排序。 You cannot predict the order of duplicate keys. 您无法预测重复键的顺序。

There are 2 generic methods to solve this problem: use a stable sort, or force uniqueness by extending the key to include identity information. 有两种通用方法可以解决此问题:通过扩展密钥以包含身份信息,使用稳定排序或强制唯一性。

One of the common stable sorts is an Insertion sort. 其中一种常见的稳定排序是插入排序。 I believe this would be the sort used by SortedList, but it does not allow duplicate keys. 我相信这将是SortedList使用的排序,但它不允许重复键。 Failing that, you can write your own, either in Linq or by hand. 如果做不到这一点,你可以用Linq或手工编写自己的。 Even a bubble sort is stable! 即使是冒泡也很稳定!

The preferred way is to keep the item identity. 首选方法是保留项目标识。 Create a list of pairs, where each pair consists of the key value and its position in the list. 创建一个对列表,其中每对由键值及其在列表中的位置组成。 Sort the list of pairs, or insert the pairs directly into a sorted list, because every pair is unique. 对对列表进行排序,或将对直接插入到排序列表中,因为每对都是唯一的。 The ordering of these pairs, and the keys they contain, is guaranteed the same on all platforms. 这些对的排序及其包含的密钥在所有平台上都保证相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM