zoukankan      html  css  js  c++  java
  • List<T>.Sort() 排序的用法

    List<T> 可以通过 .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<T> < IList<T> < ICollection<T> < IEnumerable<T>

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

  • 相关阅读:
    导入导出excel
    sql里的常用方法
    mybatis_plus
    Shiro框架从入门到实战
    PHP面向对象程序设计(视频学习)
    Java爬虫技术快速入门
    微信公众号开发客服消息与模板消息开发(视频java版)
    微信小程序从基础到实战完整视频教程
    微信扫码支付视频课程(Java版)
    支付宝web商城支付(视频java版)
  • 原文地址:https://www.cnblogs.com/wwj1992/p/5650957.html
Copyright © 2011-2022 走看看