简体   繁体   English

排序清单 <List<MyType> &gt;

[英]Sorting List<List<MyType>>

I have 2D a list of "NameValuePair"s that I've been trying to order with no luck so far. 到目前为止,我一直在尝试2D列表“ NameValuePair”,但没有运气。

NameValuePair is defined as: NameValuePair定义为:

public class NameValuePair
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Value { get; set; }
    }

The list is defined as: 该列表定义为:

List<List<NameValuePair>> outerList = new List<List<NameValuePair>>();

Each list in outer list might have different number of items at different indices but each one has a "Date" item for sure. 外部列表中的每个列表在不同的索引处可能具有不同数量的项目,但是每个列表肯定都有一个“日期”项目。

eg 例如

List<List<NameValuePair>> outerList = new List<List<NameValuePair>>();
List<NameValuePair> innerList = new List<NameValuePair>();
List<NameValuePair> innerList2 = new List<NameValuePair>();


innerList.Add(new NameValuePair { Name = "List1Item1", Value = "someValue" });
innerList.Add(new NameValuePair { Name = "List1Item2", Value = "otherValue" });
innerList.Add(new NameValuePair { Name = "List1ItemN", Value = "anotherValue" });
innerList.Add(new NameValuePair { Name = "Date", Value = "aDateInStringFormat" });
innerList2.Add(new NameValuePair { Name = "List2Item1", Value = "myValue" });
innerList2.Add(new NameValuePair { Name = "Date", Value = "anotherDateInStringFormat" });
innerList2.Add(new NameValuePair { Name = "List2ItemM", Value = "bestValue" });

outerList.Add(innerList);
outerList.Add(innerList2);

I have tried sorting with outerList.Sort(); 我尝试过用outerList.Sort();排序outerList.Sort(); and outerList.OrderByDescending(x => x.Where(y => y.Name == "Date")).ToList(); outerList.OrderByDescending(x => x.Where(y => y.Name == "Date")).ToList(); with no luck so far. 到目前为止没有运气。

I also tried implementing IComparable to my NameValuePair type by overloading CompareTo() but couldn't get it working either. 我也尝试通过重载CompareTo()IComparable实现为NameValuePair类型,但也无法使其正常工作。

Any suggestions are more than welcome. 任何建议都值得欢迎。

Assuming each inner list has exactly one item with name Date and a proper formated date Value : 假设每个内部列表都只有一个名称为Date且格式正确的date Value

var sorted = outerList.OrderBy(x => DateTime.Parse(x.Single(y => y.Name == "Date").Value))
                      .ToList();

The Linq query takes the NameValuePair with the Name "Date", converts the Value to a DateTime object and sorts the outer list by this value. Linq查询采用Name “ Date”的NameValuePair ,将Value转换为DateTime对象,并按此值对外部列表进行排序。

Anyway you should think about creating a class with a DateTime property instead. 无论如何,您应该考虑使用DateTime属性创建一个类。

I know this has already been answered, but here's a slightly different take which is likely to be more performant. 我知道这已经得到了答案,但这是一个略有不同的方法,可能会更有效。

Firstly, do a single pass of all the lists to extract the date times into a separate sequence: 首先,对所有列表进行一次传递,以将日期时间提取到一个单独的序列中:

var keys = outerList.Select(x => DateTime.Parse(x.Single(y => y.Name == "Date").Value));

Then use Zip and that DateTime sequence to sort the outer list by that sequence: 然后使用Zip和该DateTime序列按该序列对外部列表进行排序:

outerList = outerList.Zip(keys, (pairs, date) => new {Pairs = pairs, Date = date})
    .OrderByDescending(item => item.Date)
    .Select(item => item.Pairs)
    .ToList();

This avoid multiple calls to IEnumerable.Single() and DateTime parsing whenever two elements are compared during the sorting. 这样可以避免在排序期间比较两个元素时多次调用IEnumerable.Single()和DateTime解析。

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

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