日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
C#數(shù)組排序與對(duì)象大小比較

  從個(gè)小例子開(kāi)始:

創(chuàng)新互聯(lián)公司是一家網(wǎng)站建設(shè)、成都做網(wǎng)站,提供網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),網(wǎng)站制作,建網(wǎng)站,按需開(kāi)發(fā),網(wǎng)站開(kāi)發(fā)公司,于2013年成立是互聯(lián)行業(yè)建設(shè)者,服務(wù)者。以提升客戶品牌價(jià)值為核心業(yè)務(wù),全程參與項(xiàng)目的網(wǎng)站策劃設(shè)計(jì)制作,前端開(kāi)發(fā),后臺(tái)程序制作以及后期項(xiàng)目運(yùn)營(yíng)并提出專業(yè)建議和思路。

 
 
 
  1.   int[] intArray = new int[]{2,3,6,1,4,5};  
  2.   Array.Sort(intArray);  
  3.   Array.ForEach(intArray,(i)=>Console.WriteLine(i)); 

  這個(gè)例子定義了一個(gè)int數(shù)組,然后使用Array.Sort(arr)靜態(tài)方法對(duì)此數(shù)組進(jìn)行排序,最后輸出排序后的數(shù)組。以上例子將毫無(wú)意外的依次輸出1,2,3,4,5,6.

  為什么Array的Sort方法可以正確的對(duì)int數(shù)組進(jìn)行排序呢,我們自定義類可以嗎?試試看,如下代碼:

 
 
 
  1.   public class Student  
  2.   {  
  3.   public int Age { get; set; }  
  4.   public string Name { get; set; }  
  5.   public int Score { get; set; }  
  6.   }  
  7.   static void Main(string[] args)  
  8.   {  
  9.   Student[] students = new Student[]{  
  10.   new Student(){Age = 10,Name="張三",Score=70},  
  11.   new Student(){Age = 12,Name="李四",Score=97},  
  12.   new Student(){Age = 11,Name="王五",Score=80},  
  13.   new Student(){Age = 9,Name="趙六",Score=66},  
  14.   new Student(){Age = 12,Name="司馬",Score=90},  
  15.   };  
  16.   Console.WriteLine("--------------默認(rèn)排序輸出--------");  
  17.   Array.Sort(students);  
  18.   Array.ForEach(students,(s)=>Console.WriteLine(string.Format("{0}{1,2}歲了,他的分?jǐn)?shù)是{2,3}",s.Name,s.Age,s.Score)));  
  19.   Console.Read();  
  20.   } 

  我們定義了Student類然后同樣對(duì)他的數(shù)組進(jìn)行排序,程序正確的編譯通過(guò),但是運(yùn)行出錯(cuò),運(yùn)行時(shí)拋出了異常:System.InvalidOperationException{"Failed to compare two elements in the array."},這個(gè)異常的InnerException是ArgumentException{"At least one object must implement IComparable."};運(yùn)行時(shí)異常說(shuō)明:我們要使用Array.Sort(arr)靜態(tài)方法,必須得保證數(shù)組中有一個(gè)元素實(shí)現(xiàn)IComparable接口。既然如此我們就讓Student類實(shí)現(xiàn)IComparable接口.

 
 
 
  1.   public class Student :IComparable  
  2.   {  
  3.   public int Age { get; set; }  
  4.   public string Name { get; set; }  
  5.   public int Score { get; set; }  
  6.   ///   
  7.   /// 實(shí)現(xiàn)IComparable接口,用Age做比較  
  8.   ///   
  9. /// 比較對(duì)象  
  10.   /// 比較結(jié)果  
  11.   public int CompareTo(object obj)  
  12.   {  
  13.   if (obj is Student)  
  14.   {  
  15.   return Age.CompareTo(((Student)obj).Age);  
  16.   }  
  17.   return 1;  
  18.   }  
  19.   } 

  在Student類中實(shí)現(xiàn)了IComparable接口,在CompareTo方法中比較Student的Age屬性,這一次再次編譯運(yùn)行,程序正常的輸出了按照年齡排序的Student數(shù)組。

  假如說(shuō)我們要對(duì)Student的Score屬性進(jìn)行排序該怎么辦呢? Student類實(shí)現(xiàn)的IComparable接口只能按照一種屬性排序呀。

  這個(gè)是很容易實(shí)現(xiàn)的.net的類庫(kù)開(kāi)發(fā)者早為我們準(zhǔn)備了另一個(gè)接口IComparer接口用來(lái)實(shí)現(xiàn)比較類型T的兩個(gè)實(shí)例。如下StudentScoreComparer類實(shí)現(xiàn)了對(duì)Student按照Score屬性比較的IComparer

 
 
 
  1.   public class StudentScoreComparer : IComparer  
  2.   {  
  3.   public int Compare(Student x, Student y)  
  4.   {  
  5.   return x.Score.CompareTo(y.Score);  
  6.   }  
  7.  } 

  現(xiàn)在我們可以使用下面代碼對(duì)Student數(shù)組按照Score屬性進(jìn)行排序:

  Console.WriteLine("----------按分?jǐn)?shù)排序輸出------------");

  Array.Sort(students, new StudentScoreComparer());

  Array.ForEach(students, (s) => Console.WriteLine(string.Format("{0}{1,2}歲了,他的分?jǐn)?shù)是{2,3}", s.Name, s.Age, s.Score)));

  不過(guò)一個(gè)簡(jiǎn)單的按照Score屬性排序,再定義一個(gè)類是不是有點(diǎn)大題小作呀,有沒(méi)有更好的辦法呢?當(dāng)然有. .net為我們準(zhǔn)備了比較對(duì)象大小的委托Comparison我們可以使用拉姆達(dá)表達(dá)式或者匿名委托直接排序,如下代碼實(shí)現(xiàn):

 
 
 
  1.   Console.WriteLine("----------按分?jǐn)?shù)排序輸出----------");  
  2.   Array.Sort(students, (s1, s2) => s1.Score.CompareTo(s2.Score));  
  3.   Array.ForEach(students, (s) => Console.WriteLine(string.Format("{0}{1,2}歲了,他的分?jǐn)?shù)是{2,3}", s.Name, s.Age, s.Score))); 

  完整代碼示例如下:

 
 
 
  1.   using System;  
  2.   using System.Collections.Generic;  
  3.   using System.Linq;  
  4.   using System.Text;  
  5.   namespace SortingInCSharp  
  6.   {  
  7.   class Program  
  8.   {  
  9.   public class Student : IComparable  
  10.   {  
  11.   public int Age { get; set; }  
  12.   public string Name { get; set; }  
  13.   public int Score { get; set; }  
  14.   ///   
  15.   /// 實(shí)現(xiàn)IComparable接口,用Age做比較  
  16.   ///   
  17.   /// 比較對(duì)象  
  18.   /// 比較結(jié)果  
  19.   public int CompareTo(object obj)  
  20.   {  
  21.   if (obj is Student)  
  22.  {  
  23.   return Age.CompareTo(((Student)obj).Age);  
  24.   }  
  25.   return 1;  
  26.   }  
  27.   }  
  28.   static void Main(string[] args)  
  29.   {  
  30.   Student[] students = new Student[]{  
  31.   new Student(){Age = 10,Name="張三",Score=70},  
  32.   new Student(){Age = 12,Name="李四",Score=97},  
  33.   new Student(){Age = 11,Name="王五",Score=80},  
  34.   new Student(){Age = 9,Name="趙六",Score=66},  
  35.   new Student(){Age = 12,Name="司馬",Score=90},  
  36.   };  
  37.   Console.WriteLine("--------------默認(rèn)排序輸出--------");  
  38.   Array.Sort(students);  
  39.   Array.ForEach(students, (s) => Console.WriteLine(string.Format("{0}{1,2}歲了,他的分?jǐn)?shù)是{2,3}", s.Name, s.Age, s.Score)));  
  40.   Console.WriteLine("----------按分?jǐn)?shù)排序輸出------------");  
  41.   Array.Sort(students, new StudentScoreComparer());  
  42.   Array.ForEach(students, (s) => Console.WriteLine(string.Format("{0}{1,2}歲了,他的分?jǐn)?shù)是{2,3}", s.Name, s.Age, s.Score)));  
  43.   Console.WriteLine("----------按分?jǐn)?shù)排序輸出----------");  
  44.   Array.Sort(students, (s1, s2) => s1.Score.CompareTo(s2.Score));  
  45.   Array.ForEach(students, (s) => Console.WriteLine(string.Format("{0}{1,2}歲了,他的分?jǐn)?shù)是{2,3}", s.Name, s.Age, s.Score)));  
  46.   Console.Read();  
  47.   }  
  48.   public class StudentScoreComparer : IComparer  
  49.   {  
  50.   public int Compare(Student x, Student y)  
  51.   {  
  52.   return x.Score.CompareTo(y.Score);  
  53.   }  
  54.   }  
  55.   }  
  56.   } 

  總結(jié):

  在C#中有三個(gè)關(guān)于比較對(duì)象大小的接口,分別是IComparable、IComparable和IComparer。 IComparable和IComparable是類本身實(shí)現(xiàn)的在實(shí)例之間比較大小的行為定義。IComparer是定義在被比較類之外的專門比較兩個(gè)T類型對(duì)象大小的行為,另外還有一個(gè)用于比較的委托定義Comparison可以讓我們用拉姆達(dá)表達(dá)式或者匿名委托或方法更方便的排序。

原文鏈接:http://www.cnblogs.com/yukaizhao/archive/2011/08/19/csharp-compare.html

【責(zé)任編輯:彭凡 TEL:(010)68476606】


分享文章:C#數(shù)組排序與對(duì)象大小比較
標(biāo)題路徑:http://www.5511xx.com/article/cdpjedg.html