C#中对集合排序的三种方式

 更新时间:2022年09月24日 08:35:31   作者:Darren Ji  
这篇文章介绍了C#中对集合排序的三种方式,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

对集合排序,可能最先想到的是使用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方法的参数。

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

  • C#检测pc光驱里是否插入了光盘的方法

    C#检测pc光驱里是否插入了光盘的方法

    这篇文章主要介绍了C#检测pc光驱里是否插入了光盘的方法,涉及C#针对光驱等硬件检测操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • WPF实现可视化扫码器的示例代码

    WPF实现可视化扫码器的示例代码

    AForge.NET 是一个专门为开发者和研究者基于C#框架设计的,他包括计算机视觉与人工智能,图像处理,神经网络,遗传算法,机器学习,模糊系统,机器人控制等领域。本文就将用它编写一个可视化扫码器,感兴趣的可以了解一下
    2022-11-11
  • C#中感叹号(!)的一些常见用法小结

    C#中感叹号(!)的一些常见用法小结

    在C#中,感叹号(!)有多种用途,具体取决于上下,文本文主要介绍了C#中感叹号(!)的一些常见用法小结,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • C#仿Windows XP自带的扫雷游戏

    C#仿Windows XP自带的扫雷游戏

    这篇文章主要为大家详细介绍了C#仿Windows XP自带的扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • DevExpress实现GridView当无数据行时提示消息

    DevExpress实现GridView当无数据行时提示消息

    这篇文章主要介绍了DevExpress实现GridView当无数据行时提示消息,需要的朋友可以参考下
    2014-08-08
  • C#中List和数组之间转换的方法

    C#中List和数组之间转换的方法

    这篇文章主要介绍了C#中List和数组之间转换的方法,涉及比较简单的转换技巧,需要的朋友可以参考下
    2015-02-02
  • C#实现图片轮播功能的示例代码

    C#实现图片轮播功能的示例代码

    这篇文章主要为大家详细介绍了如何利用C#实现图片轮播功能,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下
    2022-12-12
  • C#面向对象的23种设计模式介绍

    C#面向对象的23种设计模式介绍

    这篇文章介绍了C#面向对象的23种设计模式,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-02-02
  • C# 设计模式系列教程-装饰模式

    C# 设计模式系列教程-装饰模式

    每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中。它是由Decorator的SetComponent方法来实现的,因而它们的职责是单一的。
    2016-06-06
  • C#使用GDI+实现生成验证码

    C#使用GDI+实现生成验证码

    这篇文章介绍了C#使用GDI+实现生成验证码的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05

最新评论