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

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

 更新時間:2015年02月22日 09:16:10   投稿:hebedich  
這篇文章主要介紹了C#的一些基礎(chǔ)知識,主要是IComparable用法,實現(xiàn)List<T>.sort()排序,非常的實用,這里推薦給大家。

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

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

復(fù)制代碼 代碼如下:

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

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

復(fù)制代碼 代碼如下:

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接口,我們該如何實現(xiàn)排序呢??梢允褂肔inq來實現(xiàn)。其實效果是一樣的,只是如果類的集合要經(jīng)常排序的話,建議使用繼承接口的方法,這樣可以簡化sort的代碼,而且更容易讓人看懂。

復(fù)制代碼 代碼如下:

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

相關(guān)文章

最新評論

革吉县| 穆棱市| 方城县| 尼木县| 中牟县| 武威市| 平和县| 石阡县| 双柏县| 米脂县| 肃宁县| 定州市| 张北县| 中西区| 萨嘎县| 浠水县| 旌德县| 巫溪县| 义乌市| 桦甸市| 罗山县| 宝鸡市| 西青区| 鹤庆县| 乌兰察布市| 水城县| 中方县| 即墨市| 潜山县| 收藏| 和硕县| 北海市| 昭平县| 固镇县| 吉隆县| 涿鹿县| 安岳县| 临安市| 泊头市| 阿拉善左旗| 昌吉市|