Search code examples
c#listsortingicomparable

Sorting List<List<MyType>>


I have 2D a list of "NameValuePair"s that I've been trying to order with no luck so far.

NameValuePair is defined as:

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.

e.g.

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(); and 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.

Any suggestions are more than welcome.


Solution

  • Assuming each inner list has exactly one item with name Date and a proper formated 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.

    Anyway you should think about creating a class with a DateTime property instead.