我如何排序列表< T>基于T的属性? [英] How can I sort List<T> based on properties of T?
本文介绍了我如何排序列表< T>基于T的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码看起来是这样的:
My Code looks like this :
Collection<NameValueCollection> optionInfoCollection = ....
List<NameValueCollection> optionInfoList = new List<NameValueCollection>();
optionInfoList = optionInfoCollection.ToList();
if(_isAlphabeticalSoting)
Sort optionInfoList
我试过optionInfoList的.sort(),但它不工作。
I tried optionInfoList.Sort() but it is not working.
推荐答案
使用排序方法和lambda表达式,它是很容易。
Using the sort method and lambda expressions, it is really easy.
myList.Sort((a, b) => String.Compare(a.Name, b.Name))
上面的例子说明如何通过你的对象类型的名称属性进行排序,假设名称为字符串类型。
The above example shows how to sort by the Name property of your object type, assuming Name is of type string.
这篇关于我如何排序列表< T>基于T的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文