-4

I have a requirement to sort a List<List<T>> say #myList. T has the properties QuestionName and Response.

I need to sort the #mylist based on response to a particular question. Here's my code:

public class T
{
    public string QuestionName {get; set;}
    public string Response {get; set;}
}

List<List<T>> myList = new List<List<T>>();

List<T> tList = new List<T>();    
tList.add({QuestionName = "FirstName", Response = "BBB"});
tList.add({QuestionName = "LastName", Response = "BBBLastName"});
myList.add(tList);

tList = new List<T>();
tList.add({QuestionName = "FirstName", Response = "AAA"});
tList.add({QuestionName = "LastName", Response = "AAACLastName"});
myList.add(tList);

tList = new List<T>();
tList.add({QuestionName = "FirstName", Response = "CCC"});
tList.add({QuestionName = "LastName", Response = "CCCLastName"});
myList.add(tList);

This basically corresponds to a table - where each T is a cell, List<T> is a row and List<List<T>> is the table.

I need to sort rows of the table (List<T>) based on the response to the FirstName question.

I searched web, but did not find a single post about sorting List<List<T>>. Is it even possible?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Sri
  • 31
  • 2
  • 8

3 Answers3

2

Following is the working version of what you expect:

class T
{
    public string QuestionName { get; set;}

    public string Response { get; set;}
 }


void Main()
{
    List<List<T>> myList = new List<List<T>>();

    List<T> tList = new List<T>();
    tList.Add(new T { QuestionName = "FirstName", Response = "BBB"});
    tList.Add(new T{ QuestionName = "LastName", Response = "BBBLastName"});
    myList.Add(tList.OrderBy(x => x.Response).ToList());

    List<T> tList1 = new List<T>();
    tList1.Add(new T{ QuestionName = "FirstName", Response = "AAA"});
    tList1.Add(new T{ QuestionName = "LastName", Response = "AAACLastName"});
    myList.Add(tList1.OrderBy(x => x.Response).ToList());

    List<T> tList2 = new List<T>();
    tList2.Add(new T{ QuestionName = "FirstName", Response = "CCC"});
    tList2.Add(new T{ QuestionName = "LastName", Response = "CCCLastName"});
    myList.Add(tList2.OrderBy(x => x.Response).ToList());

    myList =
    myList.OrderBy(list => list.First().Response).ToList();
}

How it Works:

  1. Each Row or sub list is ordered and arranged using the Response property
  2. For List<List<T>, we use the list.First().Response for ordering.
  3. Print is created using LinqPad - Dump API, you may though some other convenient API

Following is the final result:

enter image description here

Mrinal Kamboj
  • 11,300
  • 5
  • 40
  • 74
  • Thanks Mrinal, I will make sure to include a working version of code from next time – Sri Nov 07 '18 at 06:15
  • I assume this satisfies your expected use case – Mrinal Kamboj Nov 07 '18 at 06:15
  • Thanks for the information Mrinal, I tried myList.OrderBy(list => list.First().Response) and that doesn't seem to be working for me. I also tried myList.OrderBy(x => x.Find(y => y.QuestionName.ToLower().Contains("firstname")).Response). I am I doing something wrong? Sorry for my lack of understanding – Sri Nov 07 '18 at 06:26
  • @Sri I have provided working code to you, please check what in this code doesn't meet your expectation, The result is what I have posted, is that the expected result ? – Mrinal Kamboj Nov 07 '18 at 06:58
  • that is the expected result and I am able to get it to work, it was helpful - Thank you – Sri Nov 07 '18 at 13:27
0

Check this answer - Working fiddle

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public  void Main()
    {
        List<List<ABC>> myList = new List<List<ABC>>();

        List<ABC> tList = new List<ABC>();    
        tList.Add(new ABC(){QuestionName = "EFirstName", Response = "BBB"});
        tList.Add(new ABC(){QuestionName = "BLastName", Response = "BBBLastName"});
        myList.Add(tList);

        List<ABC> bList = new List<ABC>();    
        tList.Add(new ABC(){QuestionName = "zFirstName", Response = "BBB"});
        tList.Add(new ABC(){QuestionName = "QLastName", Response = "BBBLastName"});
        myList.Add(bList);

        Console.WriteLine("Normal List");

        foreach(var item in myList){
            foreach(var innerItem in item){
                  Console.WriteLine(innerItem.QuestionName + " | "+innerItem.Response);
            }
        }

        Console.WriteLine("++++++++++++++++++++++++");
        // Sort
        var sortedList = myList.Select(x=>x.OrderBy(y=> y.QuestionName)).ToList();


        Console.WriteLine("Sorted List");
        foreach(var item in sortedList){
            foreach(var innerItem in item){
                 Console.WriteLine(innerItem.QuestionName + " | "+innerItem.Response);
            }
        }

    }
}
SilentCoder
  • 1,970
  • 1
  • 16
  • 21
0

Given that the example does not come close to compiling, I rearranged a few things and went with a List of Dictionary.

List<Dictionary<string,string>> questions = new List<Dictionary<string,string>>();
questions.Add(new Dictionary<string,string>() { 
    { "FirstName", "BBB" },
    { "LastName", "BBBLastName" }
});
questions.Add(new Dictionary<string,string>() { 
    { "FirstName", "AAA" },
    { "LastName", "AAACLastName" }
});
questions.Add(new Dictionary<string,string>() { 
    { "FirstName", "CCC" },
    { "LastName", "CCCLastName" }
});

questions.Sort((a,b) => {
    List<string> tmp = new List<string>() { a["FirstName"], b["FirstName"] };
    tmp.Sort();
    return !a["FirstName"].Equals(tmp[0]) ? 1 : -1;
});

questions.ForEach(x => {
    x.Keys.ToList().ForEach(key => Console.WriteLine("{0}: {1}", key, x[key]));
});