良许Linux教程网 干货合集 详解C#中对集合排序的三种方式

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

这篇文章介绍了C#中对集合排序的三种方式,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

image-20221004214733020

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

class Program
{
  static void Main(string[] args)
  {
      IEnumerable result = GetStudents().OrderBy(r => r.Score);
      foreach (var item in result)
      {
          Console.WriteLine(item.Name + "--" + item.Score);
      }
      Console.ReadKey();
  }
  private static List GetStudents()
  {
      return new List()
      {
          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。

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

class Program
{
   static void Main(string[] args)
   {
       List result = GetStudents();
       result.Sort();
       foreach (var item in result)
       {
           Console.WriteLine(item.Name + "--" + item.Score);
       }
       Console.ReadKey();
   }
   private static List GetStudents()
   {
       return new List()
       {
           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
{
   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是一个密封类,我们无法让其实现IComparable接口呢?不用担心,Sort方法提供了一个重载,可以接收IComparer接口类型。

class Program
{
   static void Main(string[] args)
   {
       List result = GetStudents();
       result.Sort(new StudentSorter());
       foreach (var item in result)
       {
           Console.WriteLine(item.Name + "--" + item.Score);
       }
       Console.ReadKey();
   }
   private static List GetStudents()
   {
       return new List()
       {
           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
{
   public int Compare(Student x, Student y)
   {
       return x.Score.CompareTo(y.Score);
   }
}

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

1、使用OrderBy方法,返回IEnumerable类型。

2、让集合元素实现IComparable接口,再使用Sort方法,返回void。

3、集合元素不实现IComparable接口,针对集合元素类型写一个实现IComparer接口的类,把该类实例作为Sort方法的参数。

以上就是良许教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

137e00002230ad9f26e78-265x300
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部