根据一个列表对多个列表进行排序C# [英] Sort multiple lists based on one list C#
问题描述
对象结构
一个类具有多个数据列表.
Class
List1 of double
List2 of double
List3 of double
List4 of double
Object structure
A class has multiple lists of data.
Class
List1 of double
List2 of double
List3 of double
List4 of double
目标:根据一个列表对多个列表进行排序.例如. List1
升序排列,所有其他列表也遵循该顺序,以保持基于索引的单个点相对性.
Objective: Sort multiple lists based on one list. E.g. List1
in ascending order and all other lists to follow that order to maintain individual point relativity based on index.
我尝试过的初始实现是:
Initial implementations that I have tried are:
- 用列表1压缩
List2
,3和4,然后基于列表1进行排序.然后再次组合已排序的列表.
- Zip
List2
, 3 and 4 with List 1 and then sort based on List 1. Then combine sorted lists again.
例如
var x1 = testData.SelectMany(d => d.xData).ToList();
var y1 = modelData.SelectMany(d => d.yData).ToList();
var y2 = modelData.SelectMany(d => d.y2Data).ToList();
var sampleValues = x1.Zip(y1, (x, y) => new { X = x, Y = y }).OrderBy(v => v.X);
var sampleValues1 = x1.Zip(y2, (x, y) => new { X = x, Y2 = y }).OrderBy(v => v.X);`
//Next select X, Y from sampleValues and select Y2 from sampleValue2
- 在其他列表上尝试使用
SelectMany
,然后将其放入匿名类型.SelectMany
不适用于此,因为它需要确定的数据类型才能返回.
- Tried using
SelectMany
on different lists and then put that into an anonymous type.SelectMany
does not work with this as it needs definite data type to return.
这些方法中我缺少的任何东西,或者需要另一种方法来获得我想要达到的目标.
Anything that I am missing in these approaches or there is another approach required to get what I am trying to achieve.
对于我来说,也不能选择将所有这些数据或列表作为单独的行并将数据包含在列中的类. 这是因为我有具有这些属性的对象列表.因此,最终我想跨对象sampleData列表合并列表数据,然后对这些数据进行排序和使用.
Also having a class with all this data or lists as individual rows and data inside columns is not an option for me. This is because I have a list of objects having these properties. So eventually I want to merge list data across objects sampleData lists and then sort and use that data.
如果需要进一步的信息,请随时告诉我.
Feel free to let me know in case further information is required.
推荐答案
There's a not well-known method Array.Sort
that sorts an array according to the order of a second array. I made a small extension method that utilizes this oldie:
public static class ICollectionExtensions
{
public static IEnumerable<TSource> SortLike<TSource,TKey>(this ICollection<TSource> source,
IEnumerable<TKey> sortOrder)
{
var cloned = sortOrder.ToArray();
var sourceArr = source.ToArray();
Array.Sort(cloned, sourceArr);
return sourceArr;
}
}
您可以通过致电...
You can use this by calling ...
var list21 = list2.SortLike(list1);
此方法的优点是,尽管其中有两个ToArray()
调用,但它的速度非常快. ToArray()
创建该集合的浅表副本,该列表仅花费几毫秒的时间即可显示1000万个项目. Array.Sort
之所以快速,是因为它为数组的大小选择了最佳的排序算法.
The advantage of this method is that it's extremely fast, despite the two ToArray()
calls in it. ToArray()
creates a shallow copy of the collection, which only takes a couple of milliseconds with a list of 10 million items. Array.Sort
is fast because it selects the best sorting algorithm for the size of the array.
这篇关于根据一个列表对多个列表进行排序C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!