如何对IList< Class>进行排序? [英] How Do I Sort IList<Class>?
本文介绍了如何对IList< Class>进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
IList 没有 Sort()
函数.异黄酮可以帮助我吗?我想对自己的IList进行排序.
There's no Sort()
function for IList
. Can someoene help me with this?
I want to sort my own IList.
假设这是我的IList:
Suppose this is my IList:
public class MyObject()
{
public int number { get; set; }
public string marker { get; set; }
}
如何使用标记字符串对myobj进行排序?
How do I sort myobj using the marker string?
public void SortObject()
{
IList<MyObject> myobj = new List<MyObject>();
}
推荐答案
使用 OrderBy
示例
public class MyObject()
{
public int number { get; set; }
public string marker { get; set; }
}
IList<MyObject> myobj = new List<MyObject>();
var orderedList = myobj.OrderBy(x => x.marker).ToList();
对于不区分大小写的用户,应使用IComparer
For a case insensitive you should use a IComparer
public class CaseInsensitiveComparer : IComparer<string>
{
public int Compare(string x, string y)
{
return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
}
}
IList<MyObject> myobj = new List<MyObject>();
var orderedList = myobj.OrderBy(x => x.marker, new CaseInsensitiveComparer()).ToList();
这篇关于如何对IList< Class>进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文