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# 设计模式系列教程-观察者模式

    C# 设计模式系列教程-观察者模式

    将一个系统分割成一个一些类相互协作的类有一个不好的副作用,那就是需要维护相关对象间的一致性。我们不希望为了维持一致性而使各类紧密耦合,这样会给维护、扩展和重用都带来不便。观察者就是解决这类的耦合关系的。
    2016-06-06
  • C#实现在服务器端裁剪图片的方法

    C#实现在服务器端裁剪图片的方法

    这篇文章主要介绍了C#实现在服务器端裁剪图片的方法,涉及C#操作图片的相关技巧,需要的朋友可以参考下
    2015-04-04
  • 使用C#编写两个漂亮时钟的示例代码

    使用C#编写两个漂亮时钟的示例代码

    这篇文章主要为大家分享了两个使用C#编写的两个漂亮时钟的示例代码,文中的示例代码讲解详细,具有一定的参考价值,感兴趣的可以了解一下
    2023-07-07
  • c#使用Aspose打印文件的示例

    c#使用Aspose打印文件的示例

    这篇文章主要介绍了c#使用Aspose打印文件的示例,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-05-05
  • C# 中的委托详细解析与完整应用小结

    C# 中的委托详细解析与完整应用小结

    C#委托是一种类型安全的函数指针,允许将方法作为参数传递或赋值给变量,它在事件处理、回调和异步编程中广泛应用,本文详细介绍了委托的基本概念、用法和高级应用,感兴趣的朋友一起看看吧
    2025-03-03
  • 在Framework 4.0中:找出新增的方法与新增的类(一)

    在Framework 4.0中:找出新增的方法与新增的类(一)

    经常看到有同学在讨论Framework 4 的新特性,新方法,于是想写个程序找出framework4.0中新增的方法和类
    2013-05-05
  • C#中的char、string和StringBuilder的使用详解

    C#中的char、string和StringBuilder的使用详解

    这篇文章主要介绍了C#中的char、string和StringBuilder的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • 如何从dump文件中提取出C#源代码

    如何从dump文件中提取出C#源代码

    这篇文章主要介绍了如何从dump文件中提取出C#源代码,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03
  • C#实现字符串转换成字节数组的简单实现方法

    C#实现字符串转换成字节数组的简单实现方法

    这篇文章主要介绍了C#实现字符串转换成字节数组的简单实现方法,仅一行代码即可搞定,非常简单实用,需要的朋友可以参考下
    2015-05-05
  • c#中多线程访问winform控件的若干问题小结

    c#中多线程访问winform控件的若干问题小结

    大部分情况下都会碰到使用多线程控制界面上控件信息的问题。然而我们并不能用传统方法来解决这个问题,下面我将详细的介绍
    2013-10-10

最新评论