Lambda Expression来订购(排序)我的列表集合

时间:2013-08-14 06:14:13

标签: c# c#-4.0

我有一个类类型的列表集合,类包含以下属性。

class mymodel()
{
 public string Name{ get; set; }
 public string AMPM{ get; set; }
}
List<mymodel> mylist;

AMPM属性应包含“AM”或“PM”或“MIX”或“ - ”

我需要按照以下方式对列表集合进行排序:AM值位于顶部,然后是PM值,然后是Mix,然后是“ - ”值

如何使用Lambda订购此列表集合?

7 个答案:

答案 0 :(得分:5)

您可以添加其他媒体资源。

class mymodel {
    public string Name{ get; set; }
    public string AMPM{ get; set; }
    public int AMPM_Sort {
        get {
            if (AMPM == "AM")   return 1;
            if (AMPM == "PM")   return 2;
            if (AMPM == "MIX")  return 3;
            if (AMPM == "--")   return 4;
            return 9;
        }
    }
}
List<mymodel> mylist;
var sorted = mylist.OrderBy(x => x.AMPM_Sort);

答案 1 :(得分:2)

在您的班级和IComparable<T>覆盖中实施CompareTo(),定义您的优先顺序。然后将Lambda表达式用作:OrderBy(x => x);

class mymodel : IComparable<mymodel>
{
    public string AMPM { get; set; }

    public int System.IComparable<mymodel>.CompareTo(mymodel other)
    {
        int MyVal = AMPM == "AM" ? 1 : AMPM == "PM" ? 2 : AMPM == "MIX" ? 3 : 4;
        int OtherVal = other.AMPM == "AM" ? 1 : other.AMPM == "PM" ? 2 : other.AMPM == "MIX" ? 3 : 4;

        return MyVal.CompareTo(OtherVal);
 }

}

现在你可以做mylist.OrderBy(x => x)。即使是简单的mylist.Sort()也可以。

答案 2 :(得分:2)

基于Maarten解决方案,我会通过这种方式优化排序

    class mymodel
    {
        private string _ampm;
        public string Name{ get; set; }
        public string AMPM
        {
            get { return _ampm; }
            set 
            {
                _ampm = value;
                AMPM_Sort = AppropriateSort();
            }
        }

        public int AMPM_Sort { get; private set; }

        private int AppropriateSort()
        {
            if (AMPM == "AM")  return 1;
            if (AMPM == "PM")  return 2;
            if (AMPM == "MIX") return 3;
            return AMPM == "--" ? 4 : 9;
        }
    }
}

List<mymodel> mylist;
var sorted = mylist.OrderBy(x => x.AMPM_Sort);

答案 3 :(得分:0)

您可以编写自定义比较器来确定哪个值更高或更低。

看看here

答案 4 :(得分:0)

如果您想使用这些参数订购列表,则必须创建自定义比较器。

using System;
using System.Collections.Generic;

public class CustomComparer: IComparer<mymodel>
{
    public int Compare(mymodel x, mymodel y)
    {
        if (x == null)
        {
            if (y == null)
                return 0;
            else
                return -1;
        }
        // Add the comparison rules.
        // return 0 if are equal.
        // Return -1 if the second is greater.
        // Return 1 if the first is greater
    }
}

排序电话是:

List<mymodel> mylist;
CustomComparer cc = new CustomComparer();
mylist.Sort(cc);

答案 5 :(得分:0)

您必须覆盖Equal&amp;类的GetHashCode方法,然后您可以在列表中对其进行排序。

答案 6 :(得分:-1)

您可以将扩展方法'OrderBy'与lambda表达式一起使用。

 var collection = new List<Mymodel> {new Mymodel {AMPM = "AM", Name = "Test1"},
        new Mymodel {AMPM = "PM", Name = "Test2"},
        new Mymodel {AMPM = "AM", Name = "Test3"},
        new Mymodel {AMPM = "PM", Name = "Test4"}};

        var sorted = collection.OrderBy(p => p.AMPM);
相关问题