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

來源:互聯網
上載者:User

標籤:rabl   批量插入   插入   pointer   margin   ref   back   else   插入資料   

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

}

除聲明外, 跑步客文章均為原創,轉載請以連結形式標明本文地址
  C#基礎教程之IComparable用法,實現List.sort()排序

本文地址:  http://www.paobuke.com/develop/c-develop/pbk23133.html






相關內容C#實現的基於二進位讀寫檔案操作樣本C#利用WebClient實現兩種方式下載檔案詳解C#批量插入資料到Sqlserver中的四種方式C#遞迴演算法尋找數組中第K大的數
C#向無視窗的進程發送訊息C#直線的最小二乘法線性迴歸運算執行個體C#使用委託的步驟淺析C#鍵盤輸入斷行符號鍵實現點擊按鈕效果的方法

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.