成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C#數組排序與對象大小比較

開發 后端
我們將介紹C#數組排序與對象大小比較,包括一些實例代碼以及IComparable、IComparable<T>和IComparer<T>三大接口的用法。

  從個小例子開始:

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

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

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

  1.   public class Student  
  2.   {  
  3.   public int Age { getset; }  
  4.   public string Name { getset; }  
  5.   public int Score { getset; }  
  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("--------------默認排序輸出--------");  
  17.   Array.Sort(students);  
  18.   Array.ForEach<Student>(students,(s)=>Console.WriteLine(string.Format("{0}{1,2}歲了,他的分數是{2,3}",s.Name,s.Age,s.Score)));  
  19.   Console.Read();  
  20.   } 

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

  1.   public class Student :IComparable  
  2.   {  
  3.   public int Age { getset; }  
  4.   public string Name { getset; }  
  5.   public int Score { getset; }  
  6.   /// <summary>  
  7.   /// 實現IComparable接口,用Age做比較  
  8.   /// </summary>  
  9. /// <param name="obj">比較對象</param>  
  10.   /// <returns>比較結果</returns>  
  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類中實現了IComparable接口,在CompareTo方法中比較Student的Age屬性,這一次再次編譯運行,程序正常的輸出了按照年齡排序的Student數組。

  假如說我們要對Student的Score屬性進行排序該怎么辦呢? Student類實現的IComparable接口只能按照一種屬性排序呀。

  這個是很容易實現的.net的類庫開發者早為我們準備了另一個接口IComparer<T>接口用來實現比較類型T的兩個實例。如下StudentScoreComparer類實現了對Student按照Score屬性比較的IComparer<Student>

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

  現在我們可以使用下面代碼對Student數組按照Score屬性進行排序:

  Console.WriteLine("----------按分數排序輸出------------");

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

  Array.ForEach<Student>(students, (s) => Console.WriteLine(string.Format("{0}{1,2}歲了,他的分數是{2,3}", s.Name, s.Age, s.Score)));

  不過一個簡單的按照Score屬性排序,再定義一個類是不是有點大題小作呀,有沒有更好的辦法呢?當然有. .net為我們準備了比較對象大小的委托Comparison<T>我們可以使用拉姆達表達式或者匿名委托直接排序,如下代碼實現:

  1.   Console.WriteLine("----------按分數排序輸出----------");  
  2.   Array.Sort(students, (s1, s2) => s1.Score.CompareTo(s2.Score));  
  3.   Array.ForEach<Student>(students, (s) => Console.WriteLine(string.Format("{0}{1,2}歲了,他的分數是{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 { getset; }  
  12.   public string Name { getset; }  
  13.   public int Score { getset; }  
  14.   /// <summary>  
  15.   /// 實現IComparable接口,用Age做比較  
  16.   /// </summary>  
  17.   /// <param name="obj">比較對象</param>  
  18.   /// <returns>比較結果</returns>  
  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("--------------默認排序輸出--------");  
  38.   Array.Sort(students);  
  39.   Array.ForEach<Student>(students, (s) => Console.WriteLine(string.Format("{0}{1,2}歲了,他的分數是{2,3}", s.Name, s.Age, s.Score)));  
  40.   Console.WriteLine("----------按分數排序輸出------------");  
  41.   Array.Sort(students, new StudentScoreComparer());  
  42.   Array.ForEach<Student>(students, (s) => Console.WriteLine(string.Format("{0}{1,2}歲了,他的分數是{2,3}", s.Name, s.Age, s.Score)));  
  43.   Console.WriteLine("----------按分數排序輸出----------");  
  44.   Array.Sort(students, (s1, s2) => s1.Score.CompareTo(s2.Score));  
  45.   Array.ForEach<Student>(students, (s) => Console.WriteLine(string.Format("{0}{1,2}歲了,他的分數是{2,3}", s.Name, s.Age, s.Score)));  
  46.   Console.Read();  
  47.   }  
  48.   public class StudentScoreComparer : IComparer<Student>  
  49.   {  
  50.   public int Compare(Student x, Student y)  
  51.   {  
  52.   return x.Score.CompareTo(y.Score);  
  53.   }  
  54.   }  
  55.   }  
  56.   } 

  總結:

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

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

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

 

責任編輯:彭凡 來源: 博客園
相關推薦

2009-08-27 17:54:13

C#與Flex

2009-08-24 16:40:18

C#與VB7

2009-08-18 12:23:38

2009-06-15 11:03:10

Java語言C#語言

2009-09-07 05:40:16

C#窗體位置C#窗體大小

2009-07-31 14:04:11

C#時間比較大小

2009-08-20 17:13:37

C# FileSyst

2009-08-28 16:50:25

C# PromptPo

2009-08-28 12:41:49

靜態方法與非靜態方法

2020-09-28 08:11:14

JavaScript數據

2009-08-03 16:35:30

C#日期比較

2009-08-11 13:13:09

C#和Java比較

2009-08-11 14:57:11

比較C#和Java

2009-09-10 16:30:11

C#排序函數

2009-08-21 14:22:22

C# new和over

2009-08-26 13:07:07

C#交錯數組

2009-09-17 16:53:15

C#數組

2009-08-07 11:26:53

C#數組結構

2009-08-10 16:19:37

C#冒泡排序

2009-08-12 11:24:25

C# String對象
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 九色.com | 欧美日韩在线一区二区 | 欧美成年网站 | 国产精品18久久久久久白浆动漫 | 久久99蜜桃综合影院免费观看 | 91视在线国内在线播放酒店 | 最新免费av网站 | 日韩在线欧美 | 国产在线麻豆精品入口 | 最新中文字幕一区 | 97国产爽爽爽久久久 | 欧美激情在线一区二区三区 | 男人天堂99| 在线观看免费av片 | 亚洲精品一区在线 | 人人人人干 | 在线播放中文字幕 | 精品国产伦一区二区三区观看说明 | 成人a视频片观看免费 | av一区二区三区 | 日韩欧美在线观看 | 国产在线一区观看 | 网站黄色av | 国产午夜精品视频 | 精品一区二区三区在线视频 | 久久成人综合 | 999观看免费高清www | 色狠狠一区 | 欧美亚洲另类在线 | 在线欧美小视频 | 999免费网站 | 久久久久久中文字幕 | 亚洲精品中文字幕在线观看 | 国产成人麻豆免费观看 | 狠狠亚洲 | 99热这里都是精品 | 中文字幕 亚洲一区 | 亚洲网站在线观看 | 9色网站 | wwwsihu| 黄久久久|