我需要对一个List<List<T>>
说#myList
进行排序。 T
具有属性QuestionName
和Response
。
我需要根据对特定问题的回答对#mylist
进行排序。这是我的代码:
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);
这基本上对应于一个表-其中每个T
是一个单元格,List<T>
是一行,List<List<T>>
是表。
我需要根据对FirstName问题的回答对表(List<T>
)的行进行排序。
我搜索了网络,但没有找到关于排序List<List<T>>
的任何帖子。甚至有可能吗?
答案 0 :(得分:2)
以下是您期望的工作版本:
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();
}
Row
或子列表都使用Response
属性进行排序和排列List<List<T>
,我们使用list.First().Response
进行订购。LinqPad - Dump
API创建的,尽管您可以使用其他一些方便的API 答案 1 :(得分:0)
检查此答案-工作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);
}
}
}
}
答案 2 :(得分:0)
鉴于该示例尚未接近编译,因此我重新整理了一些内容,并使用了“词典列表”。
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]));
});