最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#對集合進行排序

 更新時間:2022年03月10日 09:48:27   作者:.NET開發(fā)菜鳥  
這篇文章介紹了C#對集合進行排序的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

先來看看下面List<T>泛型集合的排序例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomerSort
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();
            list.Add(1);
            list.Add(5);
            list.Add(2);
            list.Add(6);
            list.Add(3);
            list.Add(4);
            Console.WriteLine("*****排序前*****");
            foreach (var item in list)
            {
                Console.WriteLine(item.ToString());
            }

            list.Sort();
            Console.WriteLine("*****排序后*****");
            foreach (var item in list)
            {
                Console.WriteLine(item.ToString());
            }

            Console.ReadKey();
        }
    }
}

輸出結(jié)果:

從上面的截圖中可以看出,Sort()方法默認按照元素的大小進行從小到大的排序,為什么調(diào)用Sort()方法就能按照元素的大小進行從小到大的排序呢?其實現(xiàn)原理是什么呢?我們能不能自定義排序規(guī)則呢?帶著這些問題,我們先來看看Sort()方法的定義,在Sort()方法上面按F12轉(zhuǎn)到定義:

從截圖中可以看出,Sort()方法使用了幾個重載的方法??梢詡鬟f給它的參數(shù)有泛型委托Comparison<T> comparison和泛型接口IComparer<T> comparer,以及一個范圍值和泛型接口IComparer<T> comparer。只有集合中的元素實現(xiàn)了IComparable<T>接口,才能使用不帶參數(shù)的Sort()方法。我們在這里實現(xiàn)IComparer<T>接口來創(chuàng)建一個自定義類型的排序功能。

1、定義一個Student類,包括姓名和分數(shù)兩個屬性,可以按照姓名或分數(shù)進行排序,Student類定義如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomerSort
{
   public class Student
    {
        public string Name { get; set; }

        public double Score { get; set; }
    }
}

2、在定義一個枚舉,表示排序的種類,即是按照Name排序還是按照Score排序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomerSort
{
    /// <summary>
    /// 排序的種類
    /// </summary>
   public enum CompareType
    {
        Name,
        Score
    }
}

3、實現(xiàn)IComparer接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomerSort
{
    /// <summary>
    /// StudentComparer自定義排序規(guī)則類實現(xiàn)IComparable接口
    /// </summary>
    public class StudentComparer : IComparer<Student>
    {
        private CompareType _compareType;

        /// <summary>
        /// 通過構(gòu)造函數(shù)給_compareType賦值
        /// </summary>
        /// <param name="compareType"></param>
        public StudentComparer(CompareType compareType)
        {
            _compareType = compareType;
        }

        /// <summary>
        /// 實現(xiàn)IComparer接口的Compare
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public int Compare(Student x, Student y)
        {
            if (x == null && y == null)
            {
                return 0;
            }
            if (x == null)
            {
                return -1;
            }
            if (y == null)
            {
                return 1;
            }
            switch (_compareType)
            {
                case CompareType.Name:
                    return string.Compare(x.Name, y.Name);
                    break;
                case CompareType.Score:
                    return x.Score.CompareTo(y.Score);
                    break;
                default:
                    throw new ArgumentException("無效的比較類型");
            }
        }
    }
}

4、在Main()方法中調(diào)用:

先按照Name進行排序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomerSort
{
    class Program
    {
        static void Main(string[] args)
        {
            //List<int> list = new List<int>();
            //list.Add(1);
            //list.Add(5);
            //list.Add(2);
            //list.Add(6);
            //list.Add(3);
            //list.Add(4);
            //Console.WriteLine("*****排序前*****");
            //foreach (var item in list)
            //{
            //    Console.WriteLine(item.ToString());
            //}

            //list.Sort();
            //Console.WriteLine("*****排序后*****");
            //foreach (var item in list)
            //{
            //    Console.WriteLine(item.ToString());
            //}


            List<Student> list = new List<Student>()
            {
                new Student()
                {
                    Name="Tom",
                    Score=98
                } ,
                new Student()
                {
                    Name="Kevin",
                    Score=69
                } ,
                new Student()
                {
                    Name="Leo",
                    Score=81
                }
            };
            Console.WriteLine("*****排序前*****");
            foreach (var item in list)
            {
                Console.WriteLine(item.Name);
            }
            list.Sort(new StudentComparer(CompareType.Name));
            Console.WriteLine("*****排序后*****");
            foreach (var item in list)
            {
                Console.WriteLine(item.Name);
            }

            //Console.WriteLine("***按照Score排序***");

            Console.ReadKey();
        }
    }
}

 結(jié)果:

在按照Score進行排序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomerSort
{
    class Program
    {
        static void Main(string[] args)
        {
            //List<int> list = new List<int>();
            //list.Add(1);
            //list.Add(5);
            //list.Add(2);
            //list.Add(6);
            //list.Add(3);
            //list.Add(4);
            //Console.WriteLine("*****排序前*****");
            //foreach (var item in list)
            //{
            //    Console.WriteLine(item.ToString());
            //}

            //list.Sort();
            //Console.WriteLine("*****排序后*****");
            //foreach (var item in list)
            //{
            //    Console.WriteLine(item.ToString());
            //}


            List<Student> list = new List<Student>()
            {
                new Student()
                {
                    Name="Tom",
                    Score=98
                } ,
                new Student()
                {
                    Name="Kevin",
                    Score=69
                } ,
                new Student()
                {
                    Name="Leo",
                    Score=81
                }
            };
            //Console.WriteLine("*****排序前*****");
            //foreach (var item in list)
            //{
            //    Console.WriteLine(item.Name);
            //}
            //list.Sort(new StudentComparer(CompareType.Name));
            //Console.WriteLine("*****排序后*****");
            //foreach (var item in list)
            //{
            //    Console.WriteLine(item.Name);
            //}

            Console.WriteLine("*****排序前*****");
            foreach (var item in list)
            {
                Console.WriteLine(item.Score);
            }
            list.Sort(new StudentComparer(CompareType.Name));
            Console.WriteLine("*****排序后*****");
            foreach (var item in list)
            {
                Console.WriteLine(item.Score);
            }

            Console.ReadKey();
        }
    }
}

結(jié)果:

到此這篇關(guān)于C#對集合進行排序的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#實現(xiàn)動態(tài)數(shù)字時鐘和日歷

    C#實現(xiàn)動態(tài)數(shù)字時鐘和日歷

    這篇文章主要為大家詳細介紹了C#實現(xiàn)動態(tài)數(shù)字時鐘和日歷的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • c#在控制臺輸出彩色文字的方法

    c#在控制臺輸出彩色文字的方法

    c#在控制臺輸出彩色文字的方法,需要的朋友可以參考一下
    2013-03-03
  • Unity實現(xiàn)倒計時組件

    Unity實現(xiàn)倒計時組件

    這篇文章主要介紹了Unity實現(xiàn)倒計時組件的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • C#基于JsonConvert解析Json數(shù)據(jù)的方法實例

    C#基于JsonConvert解析Json數(shù)據(jù)的方法實例

    最近初接觸C#語言,發(fā)現(xiàn)JSON解析這塊和JAVA差異過大,下面這篇文章主要給大家介紹了關(guān)于C#基于JsonConvert解析Json數(shù)據(jù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-04-04
  • c# 代理模式

    c# 代理模式

    代理模式:為其他對象提供一種代理以控制其他對象的訪問
    2012-10-10
  • C#異步執(zhí)行任務的方法

    C#異步執(zhí)行任務的方法

    這篇文章主要介紹了C#異步執(zhí)行任務的方法,以一個簡單實例形式分析了C#異步執(zhí)行的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • C# 設(shè)計模式系列教程-適配器模式

    C# 設(shè)計模式系列教程-適配器模式

    通過適配器,客戶端可以調(diào)用同一接口,因而對客戶端來說是透明的。這樣做更簡單、更直接、更緊湊。
    2016-06-06
  • C#使用系統(tǒng)方法發(fā)送異步郵件完整實例

    C#使用系統(tǒng)方法發(fā)送異步郵件完整實例

    這篇文章主要介紹了C#使用系統(tǒng)方法發(fā)送異步郵件實現(xiàn)方法,結(jié)合完整實例形式分析了C#異步調(diào)用與郵件發(fā)送的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-07-07
  • C#中的DataSet、string、DataTable、對象轉(zhuǎn)換成Json的實現(xiàn)代碼

    C#中的DataSet、string、DataTable、對象轉(zhuǎn)換成Json的實現(xiàn)代碼

    這篇文章主要介紹了C#中的DataSet、string、DataTable、對象轉(zhuǎn)換成Json的實現(xiàn)代碼,需要的朋友可以參考下
    2014-09-09
  • C#實現(xiàn)微信紅包功能

    C#實現(xiàn)微信紅包功能

    這篇文章主要介紹了C#實現(xiàn)微信紅包功能,使用正態(tài)分布計算紅包金額,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06

最新評論

五指山市| 金坛市| 廊坊市| 石阡县| 阿克苏市| 永和县| 都安| 邳州市| 钟祥市| 五常市| 桃园市| 东平县| 称多县| 汉阴县| 潞城市| 项城市| 盐池县| 岗巴县| 南丹县| 海伦市| 新宾| 屯留县| 庆阳市| 苍山县| 承德市| 宿州市| 高邮市| 奉新县| 广州市| 博乐市| 久治县| 丰顺县| 湾仔区| 八宿县| 临清市| 奈曼旗| 永川市| 城固县| 青田县| 绥芬河市| 梅州市|