国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#基礎教程之IComparable用法,實現List<T>.sort()排序

2020-01-24 02:08:48
字體:
來源:轉載
供稿:網友

 List<T>.sort()可以實現對T的排序,比如List<int>.sort()執行后集合會按照int從小到大排序。如果T是一個自定義的Object,可是我們想按照自己的方式來排序,那該怎么辦呢,其實可以用過IComparable接口重寫CompareTo方法來實現。流程如下:

      一.第一步我們申明一個類Person但是要繼承IComparable接口:

復制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestIComparable
{
    public class Person : IComparable<Person>
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public int CompareTo(Person obj)
        {
            int result;
            if (this.Name == obj.Name && this.Age == obj.Age)
            {
                result = 0;
            }
            else
            {
                if (this.Name.CompareTo(obj.Name) > 0)
                {
                    result = 1;
                }
                else if (this.Name == obj.Name && this.Age > obj.Age)
                {
                    result = 1;
                }
                else
                {
                    result = -1;
                }
            }
            return result;
        }
        public override string ToString()
        {
            return this.Name + "-" + this.Age;
        }
    }
}

  二.然后在主函數里面調用sort方法即可.類就會按照姓名從小到大,如果姓名相同則按照年齡從小到大排序了。

復制代碼 代碼如下:

public class Program
{
    public static void Main(string[] args)
    {
        List<Person> lstPerson = new List<Person>();
        lstPerson.Add(new Person(){ Name="Bob",Age=19});
        lstPerson.Add(new Person(){ Name="Mary",Age=18});
        lstPerson.Add(new Person() { Name = "Mary", Age = 17 });
        lstPerson.Add(new Person(){ Name="Lily",Age=20});
        lstPerson.Sort();
        Console.ReadKey();
    }
}

   三,如果不繼承IComparable接口,我們該如何實現排序呢。可以使用Linq來實現。其實效果是一樣的,只是如果類的集合要經常排序的話,建議使用繼承接口的方法,這樣可以簡化sort的代碼,而且更容易讓人看懂。

復制代碼 代碼如下:

public static void Main(string[] args)
        {
            List<Person> lstPerson = new List<Person>();
            lstPerson.Add(new Person(){ Name="Bob",Age=19});
            lstPerson.Add(new Person(){ Name="Mary",Age=18});
            lstPerson.Add(new Person() { Name = "Mary", Age = 17 });
            lstPerson.Add(new Person(){ Name="Lily",Age=20});
            lstPerson.Sort((x,y) =>
            {
                int result;
                if (x.Name == y.Name && x.Age == y.Age)
                {
                    result = 0;
                }
                else
                {
                    if (x.Name.CompareTo(y.Name) > 0)
                    {
                        result = 1;
                    }
                    else if (x.Name == y.Name && x.Age > y.Age)
                    {
                        result = 1;
                    }
                    else
                    {
                        result = -1;
                    }
                }
                return result;
            });
            Console.ReadKey();
        }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 云安县| 宿州市| 三都| 长治县| 明光市| 独山县| 双牌县| 灵山县| 阿坝| 南汇区| 明星| 富川| 麟游县| 南川市| 望谟县| 梅州市| 竹山县| 邢台市| 镇坪县| 威远县| 肥西县| 景洪市| 绍兴市| 黄龙县| 株洲市| 治多县| 富锦市| 荣成市| 越西县| 岚皋县| 嵩明县| 炎陵县| 金华市| 皮山县| 八宿县| 舒城县| 康马县| 马龙县| 革吉县| 叙永县| 大新县|