阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

简单介绍C#中对集合排序的三种方式

30次阅读
没有评论

共计 2122 个字符,预计需要花费 6 分钟才能阅读完成。

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

对集合排序,可能最先想到的是使用 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 方法的参数。
  • 阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

    腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

    代金券:在阿里云专用满减优惠券

    正文完
    星哥说事-微信公众号
    post-qrcode
     0
    星锅
    版权声明:本站原创文章,由 星锅 于2024-07-24发表,共计2122字。
    转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
    【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
    阿里云-最新活动爆款每日限量供应
    评论(没有评论)
    验证码
    【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中