当前位置: 首页 > news >正文

对集合排序的三种方式

 

对集合排序,可能最先想到的是使用OrderBy方法。

 


 
    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方法的参数。

相关文章:

  • 下一代的中间件必须是支持docker规范的
  • 工作流引擎(咏南开发框架)
  • android中sqlite的query中的String[]造成sql畸形
  • 【Android QR Code】开源项目:ZXing(二)二维码编码
  • 使用CountDownTimer实现倒计时功能
  • 看图学维修mp3之电源篇65Z8\65Z5
  • CentOS 7.X 安全手记
  • 《More Effective C++:35个改善编程与设计的有效方法》(中文版)
  • POJ 1635 Subway tree systems(树同构)
  • Merkle Tree算法详解
  • 数字证书
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • java字符数组char[]和字符串String之间的转换
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • js猜数字小游戏——原创
  • Android路由框架AnnoRouter:使用Java接口来定义路由跳转
  • Docker 笔记(2):Dockerfile
  • DOM的那些事
  • echarts花样作死的坑
  • js
  • LeetCode18.四数之和 JavaScript
  • node-sass 安装卡在 node scripts/install.js 解决办法
  • PhantomJS 安装
  • Python - 闭包Closure
  • SpringCloud集成分布式事务LCN (一)
  • web标准化(下)
  • 测试如何在敏捷团队中工作?
  • 从输入URL到页面加载发生了什么
  • 规范化安全开发 KOA 手脚架
  • 基于axios的vue插件,让http请求更简单
  • 融云开发漫谈:你是否了解Go语言并发编程的第一要义?
  • HanLP分词命名实体提取详解
  • Linux权限管理(week1_day5)--技术流ken
  • 函数计算新功能-----支持C#函数
  • 好程序员大数据教程Hadoop全分布安装(非HA)
  • #laravel 通过手动安装依赖PHPExcel#
  • #多叉树深度遍历_结合深度学习的视频编码方法--帧内预测
  • %3cli%3e连接html页面,html+canvas实现屏幕截取
  • (175)FPGA门控时钟技术
  • (3)nginx 配置(nginx.conf)
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (八)Spring源码解析:Spring MVC
  • (动手学习深度学习)第13章 计算机视觉---图像增广与微调
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (附源码)ssm户外用品商城 毕业设计 112346
  • (论文阅读32/100)Flowing convnets for human pose estimation in videos
  • (转)c++ std::pair 与 std::make
  • (转)树状数组
  • (转载)hibernate缓存
  • ******IT公司面试题汇总+优秀技术博客汇总
  • ***监测系统的构建(chkrootkit )
  • ***检测工具之RKHunter AIDE
  • .Net - 类的介绍
  • .net core 6 redis操作类
  • .Net Core 中间件验签