日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Icomparer和Icomparable集合排序

發布時間:2024/9/21 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Icomparer和Icomparable集合排序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

c#中實現對象集合的排序可以使用ArrayList中的Sort()方法,而有比較才能談排序,因為不是基本類型(如string ,int.double......等)所以.NET Framework不可能一一制定他們的比較規則,那么則需要程序員自行制定,而比較規則的制定就需要通過繼承這兩個接口>之一來實現。制定了比較規則后則才可以用以下兩種方式之一調用排序:
(1)ArrayList實例.Sort(); // IComparable
(2)ArrayList實例.Sort(實現Icomparer接口的類); // Icomparer

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections;namespace Demo3 {class Program{static void Main(string[] args){//新建集合people用來存放person實例ArrayList people = new ArrayList();//建立4個person實例Person person1 = new Person("Jone", 18);Person person2 = new Person("Tom", 20);Person person3 = new Person("Lily", 15);Person person4 = new Person("July", 25);//將實例添加到people集合中people.Add(person1);people.Add(person2);people.Add(person3);people.Add(person4);//輸出原來序列Console.WriteLine("原來序列:");foreach (Person person in people){Console.WriteLine("person name: {0} age:{1}", person.Name, person.Age);}//用實現Icomparable進行排序people.Sort();//用實現Icomparable的方法輸出排序后的序列Console.WriteLine("按年齡排序后的序列:");foreach (Person person in people){Console.WriteLine("person name: {0} age:{1}", person.Name, person.Age);}//用實現Icomparer的方法進行排序people.Sort(PersonComparer.Default);//用實現Icomparer的方法輸出排序后的序列Console.WriteLine("按名稱排序后的序列:");foreach (Person person in people){Console.WriteLine("person name: {0} age:{1}", person.Name, person.Age);}Console.ReadKey();}}public class Person : IComparable{/// <summary>/// 兩個私有字段:/// 人物姓名;/// 人物年齡;/// </summary>private string name;private int age;/// <summary>/// 構造函數/// </summary>public Person(string myname, int myage){name = myname;age = myage;}/// <summary>/// 兩個共有屬性:/// 分別對應兩個私有字段;/// </summary>public string Name{set{name = value;}get{return name;}}public int Age{set{age = value;}get{return age;}}public int CompareTo(object myobject){if (myobject is Person)//用is運算符判斷要比較的對象是否是Person對象{//如果是用as運算符進行對象轉換,返回年齡比較結果(一個整數,表示兩者差)Person myperson = myobject as Person;return this.Age - myperson.Age;//return myperson.Age - this.Age;}else{//如果不是,拋出異常throw new ArgumentException("Object to compare to is not a Person Object");}}}public class PersonComparer : IComparer{//靜態字段,方便使用,沒有也可,調用方法會變public static IComparer Default = new PersonComparer();public int Compare(object myperson1, object myperson2){//用is運算符判斷要比較的對象是否都是Person對象if (myperson1 is Person && myperson2 is Person){//如果是,調用.Net Framework已經實現好的能比較基本類型的函數:Comparer.Default.Compare//(要用using System.Collections;)return Comparer.Default.Compare(((Person)myperson1).Name, ((Person)myperson2).Name);}else{//如果不是拋出異常throw new ArgumentException("One or both objects to compare are not Person objects.");}}} }

方法論:讀書加上網查詢相關資料,能夠更好的理解知識點。

本文轉自TBHacker博客園博客,原文鏈接:http://www.cnblogs.com/jiqing9006/p/6796626.html,如需轉載請自行聯系原作者

總結

以上是生活随笔為你收集整理的Icomparer和Icomparable集合排序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。