zoukankan      html  css  js  c++  java
  • 对集合排序的三种方式

    http://www.cnblogs.com/darrenji/p/4397412.html

    class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                IEnumerable<Student> result = GetStudents().OrderBy(r => r.Score);
    
                foreach (var item in result)
    
                {
    
                    Console.WriteLine(item.Name + "--" + item.Score);
    
                }
    
                Console.ReadKey();
    
            }
    
            private static List<Student> GetStudents()
    
            {
    
                return new List<Student>()
    
                {
    
                    new Student(){Id = 1, Name = "张三",Age = 15, Score = 80},
    
                    new Student(){Id = 2, Name = "李四",Age = 16, Score = 70},
    
                    new Student(){Id = 3, Name = "赵武",Age = 14, Score = 90}
    
                };
    
            }
    
        }
    
        public class Student 
    
        {
    
            public int Id { get; set; }
    
            public string Name { get; set; }
    
            public int Age { get; set; }
    
            public int Score { get; set; }
    
        }
    

    以上,OrderBy返回的类型是IEnumerable<Student>。

    如果想使用List<T>的Sort方法,就需要让Student实现IComparable<Student>接口。

       class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                List<Student> result = GetStudents();
    
                result.Sort();
    
                foreach (var item in result)
    
                {
    
                    Console.WriteLine(item.Name + "--" + item.Score);
    
                }
    
                Console.ReadKey();
    
            }
    
            private static List<Student> GetStudents()
    
            {
    
                return new List<Student>()
    
                {
    
                    new Student(){Id = 1, Name = "张三",Age = 15, Score = 80},
    
                    new Student(){Id = 2, Name = "李四",Age = 16, Score = 70},
    
                    new Student(){Id = 3, Name = "赵武",Age = 14, Score = 90}
    
                };
    
            }
    
        }
    
        public class Student : IComparable<Student>
    
        {
    
            public int Id { get; set; }
    
            public string Name { get; set; }
    
            public int Age { get; set; }
    
            public int Score { get; set; }
    
            
    
            public int CompareTo(Student other)
    
            {
    
              return  this.Score.CompareTo(other.Score);
    
            }
    
        }
    

    让Student实现IComparable<Student>接口固然很好,如果Student是一个密封类,我们无法让其实现IComparable<Student>接口呢?不用担心,Sort方法提供了一个重载,可以接收IComparer接口类型。

       class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                List<Student> result = GetStudents();
    
                result.Sort(new StudentSorter());
    
                foreach (var item in result)
    
                {
    
                    Console.WriteLine(item.Name + "--" + item.Score);
    
                }
    
                Console.ReadKey();
    
            }
    
            private static List<Student> GetStudents()
    
            {
    
                return new List<Student>()
    
                {
    
                    new Student(){Id = 1, Name = "张三",Age = 15, Score = 80},
    
                    new Student(){Id = 2, Name = "李四",Age = 16, Score = 70},
    
                    new Student(){Id = 3, Name = "赵武",Age = 14, Score = 90}
    
                };
    
            }
    
        }
    
        public class Student
    
        {
    
            public int Id { get; set; }
    
            public string Name { get; set; }
    
            public int Age { get; set; }
    
            public int Score { get; set; }
    
        }
    
        public class StudentSorter : IComparer<Student>
    
        {
    
            public int Compare(Student x, Student y)
    
            {
    
                return x.Score.CompareTo(y.Score);
    
            }
    
        }
    

    综上,如果我们想对一个集合排序,大致有三种方式:

    1、使用OrderBy方法,返回IEnumerable<T>类型。
    2、让集合元素实现IComparable<T>接口,再使用Sort方法,返回void。
    3、集合元素不实现IComparable<T>接口,针对集合元素类型写一个实现IComparer<T>接口的类,把该类实例作为Sort方法的参数。

  • 相关阅读:
    机器学习笔记19(unspervised learning -> Word Embedding)
    full-stack-fastapi-postgresql-从安装docker开始
    H3C诊断模式下判断端口是否拥塞
    pandas 数据重塑--stack,pivot
    解决Mybatis 异常:A query was run and no Result Maps were found for the Mapped Statement 'xingzhi.dao.music.ISong.GetSongTotal'
    foreach + remove = ConcurrentModificationException
    Spring MVC 实体参数默认值设置
    JDBC中SQL语句与变量的拼接
    在IDEA中使用JDBC获取数据库连接时的报错及解决办法
    使用Docker分分钟搭建漂亮的prometheus+grafana监控
  • 原文地址:https://www.cnblogs.com/chengjun/p/6322255.html
Copyright © 2011-2022 走看看