0

如何List<ABC>c1元素升序排序?非常感谢!

public class ABC
{
    public string c0 { get; set; }
    public string c1 { get; set; }
    public string c2 { get; set; }
}
public partial class MainWindow : Window
{
    public List<ABC> items = new List<ABC>();
    public MainWindow()
    {
        InitializeComponent();
        items.Add(new ABC
        {
            c0 = "1",
            c1 = "DGH",
            c2 = "yes"
        });
        items.Add(new ABC
        {
            c0 = "2",
            c1 = "ABC",
            c2 = "no"
        });
        items.Add(new ABC
        {
            c0 = "3",
            c1 = "XYZ",
            c2 = "yes"
        });
    }
}
4

4 回答 4

5

这个怎么样:

var sortedItems = items.OrderBy(i => i.c1);

这将返回一个IEnumerable<ABC>,如果您需要一个列表,请添加一个ToList

List<ABC> sortedItems = items.OrderBy(i => i.c1).ToList();
于 2013-03-17T07:16:03.947 回答
2
List<ABC> _sort = (from a in items orderby a.c1 select a).ToList<ABC>();
于 2013-03-17T07:16:16.003 回答
2

尝试类似:

var sortedItems = items.OrderBy(itm => itm.c0).ToList();  // sorted on basis of c0 property
var sortedItems = items.OrderBy(itm => itm.c1).ToList();  // sorted on basis of c1 property
var sortedItems = items.OrderBy(itm => itm.c2).ToList();  // sorted on basis of c2 property
于 2013-03-17T07:19:55.347 回答
1
.OrderBy(x => x.c1);

(或.OrderByDescending

是的,LINQ 让它变得如此简单。

于 2013-03-17T07:16:26.793 回答