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

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();
        }
    }
}

輸出結果:

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

從截圖中可以看出,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>
        /// 通過構造函數(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()方法中調用:

先按照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();
        }
    }
}

 結果:

在按照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();
        }
    }
}

結果:

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

相關文章

  • C#/VB.NET 給Excel添加、刪除數(shù)字簽名的方法

    C#/VB.NET 給Excel添加、刪除數(shù)字簽名的方法

    這篇文章主要介紹了C#/VB.NET 給Excel添加、刪除數(shù)字簽名的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • C#使用foreach語句遍歷隊列(Queue)的方法

    C#使用foreach語句遍歷隊列(Queue)的方法

    這篇文章主要介紹了C#使用foreach語句遍歷隊列(Queue)的方法,涉及foreach語句的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • unity實現(xiàn)貪吃蛇游戲

    unity實現(xiàn)貪吃蛇游戲

    這篇文章主要為大家詳細介紹了unity實現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • opencvsharp瑕疵檢測的實現(xiàn)示例

    opencvsharp瑕疵檢測的實現(xiàn)示例

    本文主要介紹了opencvsharp瑕疵檢測的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-05-05
  • c#使用htmlagilitypack解析html格式字符串

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

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

    WPF在VisualTree上增加Visual

    這篇文章介紹了WPF在VisualTree上增加Visual的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • c# 用Dictionary實現(xiàn)日志數(shù)據批量插入

    c# 用Dictionary實現(xiàn)日志數(shù)據批量插入

    這篇文章主要介紹了c# 用Dictionary實現(xiàn)日志數(shù)據批量插入的步驟,幫助大家更好的理解和使用c#中的Dictionary類,感興趣的朋友可以了解下
    2021-02-02
  • C#計算矩陣的秩實例分析

    C#計算矩陣的秩實例分析

    這篇文章主要介紹了C#計算矩陣的秩實現(xiàn)方法,以實例形式較為詳細的分析了C#計算矩陣秩的原理與實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • C#面向對象編程之猜拳游戲實現(xiàn)方法

    C#面向對象編程之猜拳游戲實現(xiàn)方法

    這篇文章主要介紹了C#面向對象編程之猜拳游戲實現(xiàn)方法,以一個完整的猜拳游戲為例講述了C#面向對象程序設計的具體實現(xiàn)步驟,具有一定的學習與借鑒價值,需要的朋友可以參考下
    2014-11-11
  • C# Lambda 知識回顧

    C# Lambda 知識回顧

    本文主要介紹了C#中Lambda的相關知識。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01

最新評論