按字段排序列表(C#) [英] Sort list by field (C#)
本文介绍了按字段排序列表(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我上过这样的课:
class SortNode
{
public Int32 m_valRating = 0;
public SortNode(Int32 valRating)
{
this.m_valRating = valRating;
}
}
和一些列表refSortNodeList
:
List<SortNode> refSortNodeList = new List<SortNode>();
Random refRandom = new Random();
for (int i = 0; i < 100; ++i)
{
refSortNodeList.Add(new SortNode(refRandom.Next(-10, 30)));
}
foreach (var varSortNode in refSortNodeList)
{
Console.WriteLine("SortNode rating is {0}", varSortNode.m_valRating);
}
如何轻松按m_valRating
字段对我的refSortNodeList
排序?还是我需要使用另一个List
类?
How to sort easily my refSortNodeList
by m_valRating
field? Or maybe I need to use some another List
class?
推荐答案
list.Sort((x,y) =>
x.m_valRating.CompareTo(y.m_valRating));
这篇关于按字段排序列表(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文