欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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#實現基于加減按鈕形式控制系統音量及靜音的方法

    這篇文章主要介紹了C#實現基于加減按鈕形式控制系統音量及靜音的方法,涉及C#引用user32.dll動態(tài)鏈接庫操作系統音量的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • C#中timer定時器用法實例

    C#中timer定時器用法實例

    這篇文章主要介紹了C#中timer定時器用法,實例分析了timer定時器實現定時觸發(fā)事件的技巧,需要的朋友可以參考下
    2015-04-04
  • C#中String.PadRight方法的具體使用

    C#中String.PadRight方法的具體使用

    本文主要介紹了C#中String.PadRight方法的具體使用, 返回一個指定長度的新字符串,其中在當前字符串的結尾填充空格或指定的Unicode字符,下面就來詳細的了解一下
    2024-01-01
  • C# Winform實現圓角無鋸齒按鈕

    C# Winform實現圓角無鋸齒按鈕

    這篇文章主要為大家詳細介紹了C# Winform實現圓角無鋸齒按鈕,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • C#列出當前系統所有正在運行程序的方法

    C#列出當前系統所有正在運行程序的方法

    這篇文章主要介紹了C#列出當前系統所有正在運行程序的方法,涉及C#操作系統進程的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • C#調用Python腳本程序的兩種方法

    C#調用Python腳本程序的兩種方法

    本文主要介紹了C#調用Python腳本程序的兩種方法,包含介紹了通過C#IronPython開源庫和通過Process類來運行python解釋器這兩種,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • C#中WebClient實現文件下載

    C#中WebClient實現文件下載

    本篇文章主要介紹了C#中WebClient實現文件下載,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • C#實現ComboBox自動匹配字符

    C#實現ComboBox自動匹配字符

    本文介紹C#如何實現ComboBox自動匹配字符1.采用CustomSource當做提示集合2. 直接使用下拉列表中的項作為匹配的集合,需要了解的朋友可以參考下
    2012-12-12
  • C#入門之索引器使用實例

    C#入門之索引器使用實例

    這篇文章主要介紹了C#的索引器使用方法,對此,C#初學者應予以牢固掌握,需要的朋友可以參考下
    2014-08-08
  • c#使用htmlagilitypack解析html格式字符串

    c#使用htmlagilitypack解析html格式字符串

    這篇文章主要介紹了c#使用htmlagilitypack解析html格式字符串的示例,需要的朋友可以參考下
    2014-03-03

最新評論