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

C# 泛型List排序的實(shí)現(xiàn)

 更新時(shí)間:2022年06月23日 14:52:30   作者:廷益--飛鳥  
本文主要介紹了C# 泛型List排序的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

本文主要介紹了C# 泛型List排序的實(shí)現(xiàn),分享給大家,具體如下:

在這里插入圖片描述

代碼

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

namespace L_List_sort
{
    public class Person:IComparable<Person>
    {
        // 屬性
        public string name; 
        public int age;

        // 構(gòu)造
        public Person(string name, int age)
        {
            this.name = name;
            this.age = age;
        }

        // 重寫字符串
        public override string ToString()
        {
            return "name: " + this.name + "  age: " + this.age;
        }

        // 實(shí)現(xiàn)比較接口
        public int CompareTo(Person other)
        {
            // 根據(jù)返回值排序  升序
            if (this.age > other.age)
            {   // 大于0 放后面
                return 1;
            }
            else
            {   // 小于 0 放前面
                return -1;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("泛型數(shù)組的排序");

            #region 知識(shí)一 List自帶排序方法
            Console.WriteLine("--------------------List自帶排序方法");
            List<int> listInt = new List<int>();

            listInt.Add(4);
            listInt.Add(2);
            listInt.Add(3);
            listInt.Add(1);
            Console.WriteLine("-------排序前");
            PrintList<int>(listInt);
            Console.WriteLine("-------排序后");
            // 排序
            listInt.Sort();
            PrintList<int>(listInt);
            #endregion

            #region 知識(shí)二 自定義類的排序
            Console.WriteLine("--------------------自定義類的排序");
            List<Person> listPerson = new List<Person>();
            listPerson.Add(new Person("張三", 20));
            listPerson.Add(new Person("李四", 18));
            listPerson.Add(new Person("王五", 31));
            listPerson.Add(new Person("曹操", 45));
            Console.WriteLine("-------排序前");
            PrintList<Person>(listPerson);
            Console.WriteLine("-------排序后");

            // 繼承排序(需要繼承 接口 :IComparable<Person>)
            listPerson.Sort();

            PrintList<Person>(listPerson);

            #endregion

            #region 知識(shí)三 通過(guò)委托函數(shù)進(jìn)行排序
            Console.WriteLine("--------------------通過(guò)委托函數(shù)進(jìn)行排序");
            listPerson.Clear();
            listPerson.Add(new Person("張三", 20));
            listPerson.Add(new Person("李四", 18));
            listPerson.Add(new Person("王五", 31));
            listPerson.Add(new Person("曹操", 45));

            Console.WriteLine("-------排序前");
            PrintList<Person>(listPerson);

            // 使用委托==>函數(shù)排序
            listPerson.Sort(SortPerson);
            Console.WriteLine("-------排序后");
            PrintList<Person>(listPerson);

            // Lambda 再次排序
            listPerson.Sort((leftP, rightP) => {
                return leftP.age > rightP.age ? 1 : -1;
            });
           
            Console.WriteLine("-------Lambda 再次排序后");
            PrintList<Person>(listPerson);
            #endregion

            Console.ReadLine();
        }

        // 排序函數(shù)
        private static int SortPerson(Person leftP, Person rightP)
        {
            // 根據(jù)返回值排序  升序
            if (leftP.age > rightP.age)
            {   // 大于0 放后面
                return -1;
            }
            else
            {   // 小于 0 放前面
                return 1;
            }

        }


        // 打印列表中元素的內(nèi)容
        private static void PrintList<T>(List<T> nList)
        {
            if (nList.Count == 0)
                Console.WriteLine("--列表為空數(shù)據(jù)");

            for (int i = 0; i < nList.Count; i++)
            {
                Console.WriteLine(nList[i].ToString());
            }
        }
    }
}

在這里插入圖片描述

 到此這篇關(guān)于C# 泛型List排序的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C# 泛型List排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論