List<T>.Sort() 排序的用法

List 可以通过 .Sort()进行排序,但是当 T 对象为自定义类型时(比如自定义模型),就需要 IComparable接口重写其中的方法来实现,实现代码如下:

class Program
    {
        static Func<Model, int> where = a => a.id;
        static Func<Model, bool> wherelambda = a => a.id < 3;
        static void Main(string[] a)
        {
            List<Model> result = GetData().ToList();
            result.Sort();          //排序
        } 

        /// <summary>
        /// 拼装数据
        /// </summary>
        /// <returns></returns>
        #region  
        public static IList<Model> GetData()
        {
            IList<Model> list = new List<Model>();
            for (int i = 0; i <= 10; i++)
            {
                Model model = new Model();
                model.id = i;
                model.Name = "名字" + i;
                model.Email = string.Format("12345@QQ{0}.com.cn", i);
                list.Add(model);
            }
            return list;
        }
        #endregion
    }

    /// <summary>
    /// 自定义模型 一定要继承 IComparable<T>接口
    /// </summary>
    public class Model : IComparable<Model>
    {
        public int id { get; set; }

        private int Sex { get; set; }

        public string Name { get; set; }

        public DateTime? BirthDate { get; set; }

        public string Email { get; set; }

        public string Phone { get; set; }

        public int CompareTo(Model model)
        {
            if (model.id == id && model.Name.Equals(Name))
                return 0;
            else
            {
                if (id > model.id)
                {
                    return -1;
                }
                if (Name.CompareTo(model.Name) > 0)
                {
                    return -1;
                }
                else
                    return 1;
            }

        }
    }

如果不继续IComparable接口,也可以直接在 .Sort()方法里面写,代码如下:

class Program
    {
        static Func<Model, int> where = a => a.id;
        static Func<Model, bool> wherelambda = a => a.id < 3;
        static void Main(string[] a)
        {
            List<Model> result = GetData().ToList();
            result.Sort((a,b)=>{
                if (b.id == a.id && b.Name.Equals(a.Name))
                    return 0;
                else
                {
                    if (a.id > b.id)
                    {
                        return -1;
                    }
                    if (a.Name.CompareTo(b.Name) > 0)
                    {
                        return -1;
                    }
                    else
                        return 1;
                }
            });          //排序
        }

按照功能排序:List < IList < ICollection < IEnumerable

按照性能排序:IEnumerable < ICollection < IList < List

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值