C# 对列表进行排序<;列表<;T>&燃气轮机;
我需要对C# 对列表进行排序<;列表<;T>&燃气轮机;,c#,C#,我需要对列表进行排序比如#我的列表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>>(); L
列表进行排序
比如#我的列表
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
{
公共字符串QuestionName{get;set;}
公共字符串响应{get;set;}
}
List myList=新列表();
List tList=新列表();
添加({QuestionName=“FirstName”,Response=“BBB”});
添加({QuestionName=“LastName”,Response=“BBBLastName”});
myList.add(tList);
tList=新列表();
添加({QuestionName=“FirstName”,Response=“AAA”});
添加({QuestionName=“LastName”,Response=“AAACLastName”});
myList.add(tList);
tList=新列表();
添加({QuestionName=“FirstName”,Response=“CCC”});
添加({QuestionName=“LastName”,Response=“cclastname”});
myList.add(tList);
这基本上对应于一个表格,其中每个T
是一个单元格,List
是一行,List
是表格
我需要根据对名字问题的回答对表中的行(List
)进行排序
我在网上搜索,但没有找到一篇关于排序的帖子。甚至可能吗?以下是您期望的工作版本:
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();
}
T类
{
公共字符串QuestionName{get;set;}
公共字符串响应{get;set;}
}
void Main()
{
List myList=新列表();
List tList=新列表();
添加(新的T{QuestionName=“FirstName”,Response=“BBB”});
添加(新的T{QuestionName=“LastName”,Response=“BBBLastName”});
添加(tList.OrderBy(x=>x.Response.ToList());
List tList1=新列表();
tList1.Add(新的T{QuestionName=“FirstName”,Response=“AAA”});
tList1.Add(newt{QuestionName=“LastName”,Response=“AAACLastName”});
Add(tList1.OrderBy(x=>x.Response.ToList());
List tList2=新列表();
tList2.Add(新的T{QuestionName=“FirstName”,Response=“CCC”});
tList2.Add(新的T{QuestionName=“LastName”,Response=“ccclatname”});
Add(tList2.OrderBy(x=>x.Response.ToList());
迈利斯特=
myList.OrderBy(list=>list.First().Response.ToList();
}
工作原理:
行
或子列表都使用响应
属性进行排序和排列列表检查此答案-工作
使用系统;
使用System.Collections.Generic;
使用System.Linq;
公共课程
{
公共图书馆
{
List myList=新列表();
List tList=新列表();
添加(new ABC(){QuestionName=“EFirstName”,Response=“BBB”});
添加(新ABC(){QuestionName=“BLastName”,Response=“BBBLastName”});
myList.Add(tList);
List bList=新列表();
添加(new ABC(){QuestionName=“zFirstName”,Response=“BBB”});
添加(new ABC(){QuestionName=“QLastName”,Response=“BBBLastName”});
myList.Add(bList);
Console.WriteLine(“正常列表”);
foreach(myList中的var项){
foreach(项目中的变量innerItem){
Console.WriteLine(innerItem.QuestionName+“|”+innerItem.Response);
}
}
Console.WriteLine(“+++++++++++++++++++++++++++++++++++++++++”;
//分类
var sortedList=myList.Select(x=>x.OrderBy(y=>y.QuestionName)).ToList();
Console.WriteLine(“排序列表”);
foreach(分类列表中的var项目){
foreach(项目中的变量innerItem){
Console.WriteLine(innerItem.QuestionName+“|”+innerItem.Response);
}
}
}
}
考虑到示例与编译不太接近,我重新安排了一些内容,并列出了字典
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]));
});
列出问题=新建列表();
添加(新字典(){
{“名字”,“BBB”},
{“LastName”,“BBBLastName”}
});
添加(新字典(){
{“名字”,“AAA”},
{“LastName”,“AAACLastName”}
});
添加(新字典(){
{“FirstName”,“CCC”},
{“LastName”,“cclastname”}
});
问题。排序((a,b)=>{
List tmp=newlist(){a[“FirstName”],b[“FirstName”]};
tmp.Sort();
return!a[“FirstName”]。等于(tmp[0])?1:-1;
});
问题。ForEach(x=>{
x、 ForEach(key=>Console.WriteLine(“{0}:{1}”,key,x[key]);
});
可能重复使用Linq的OrderBy扩展功能,这篇文章谈到了排序列表,我正在尝试排序列表我不太清楚您的确切要求,但是您可以为list
做的是myList.OrderBy(list=>list.First())
如果您能提供一个感谢邮件,那就太棒了,我将确保在下次我假设它满足您的预期用例时包含一个工作版本的代码感谢您提供的信息最后,我尝试了myList.OrderBy(list=>list.First().Response),但这似乎对我不起作用。我还尝试了myList.OrderBy(x=>x.Find(y=>y.QuestionName.ToLower().Contains(“firstname”)).Response)。我做错什么了吗?对不起,我没有时间understanding@Sri我已经向您提供了工作代码,请检查此代码中的哪些内容不符合您的期望,结果就是我发布的结果,这是预期的结果吗?这是预期的结果,我能够让它工作,这很有帮助-谢谢
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]));
});