C#高级编程(5) Array .Sort()、IComparable 、IComparer

来源:互联网 发布:三国志3优化版1.76 编辑:程序博客网 时间:2024/06/07 16:18
Array 类实现了对数组中元素的冒泡排序。Sort()方法需要数组中的元素实现 IComparable 接口。
简单类型,如 System.String 和 System.Int32 实现了 IComparable 接口,所以可以对包含这些类型的元

素排序。 

如对要对person类使用Sort();

public class Person : IComparable  {private string firstName,lastName;public Person(){}public Person(string firstName,string lastName){this.firstName=firstName;this.lastName=lastName;}public string FirstName{get {return this.firstName;}}public string LastName{get {return this.lastName;}}public override string ToString(){return String.Format("{0} {1}",firstName,lastName);}public int CompareTo(object obj)  //实现IComparable 接口CompareTo方法{Person other = obj as Person;int result=this.LastName.CompareTo(other.LastName); //string类实现了IComparable接口if(result==0){result=this.FirstName.CompareTo(other.FirstName);}return result;}};public class myReverserClass : IComparer  {      // Calls CaseInsensitiveComparer.Compare with the parameters reversed.        int IComparer.Compare( Object x, Object y )  {     //实现IComparer 接口            return( (new CaseInsensitiveComparer()).Compare( y, x ) );//CaseInsensitiveComparer比较两个对象是否相等,比较时忽略字符串的大小写。        }static void Main(){Person[] persons={new Person("Emerson","Fittipaldi"),new Person("Niki","Lauda"),new Person("Ayrton","Senna"),new Person("Michael","Schumacher")};IComparer myIComparer=new myReverserClass();//Sort()方法需要数组中的元素实现 IComparable 接口。//可以使用默认的排序,也可以使用指定的 IComparer,对一维 Array 的元素进行排序。如下 Array.Sort( persons);  //使用默认的排序// Array.Sort( persons,myIComparer);//按指定的 IComparer接口排序  foreach(Person p in persons)  {Console.WriteLine(p);  }}



如果 Person 对象的排序方式与上述不同,或者不能修改在数组中用作元素的类,就可以执行
IComparer 接口。这个接口定义了方法 Compare()。IComparable 接口必须由要比较的类来执行,而
IComparer 接口独立于要比较的类。 这就是 Compare()方法定义了两个要比较的变元的原因。 其返回值
与 IComparable 接口的 CompareTo()方法类似。



摘自msdn:

public class SamplesArray  {   public class myReverserClass : IComparer  {      // Calls CaseInsensitiveComparer.Compare with the parameters reversed.      int IComparer.Compare( Object x, Object y )  {          return( (new CaseInsensitiveComparer()).Compare( y, x ) );      }   }   public static void Main()  {      // Creates and initializes a new Array and a new custom comparer.      String[] myArr = { "The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog" };      IComparer myComparer = new myReverserClass();      // Displays the values of the Array.      Console.WriteLine( "The Array initially contains the following values:" );      PrintIndexAndValues( myArr );      // Sorts a section of the Array using the default comparer.      Array.Sort( myArr, 1, 3 );      Console.WriteLine( "After sorting a section of the Array using the default comparer:" );      PrintIndexAndValues( myArr );      // Sorts a section of the Array using the reverse case-insensitive comparer.      Array.Sort( myArr, 1, 3, myComparer );      Console.WriteLine( "After sorting a section of the Array using the reverse case-insensitive comparer:" );      PrintIndexAndValues( myArr );      // Sorts the entire Array using the default comparer.      Array.Sort( myArr );      Console.WriteLine( "After sorting the entire Array using the default comparer:" );      PrintIndexAndValues( myArr );      // Sorts the entire Array using the reverse case-insensitive comparer.      Array.Sort( myArr, myComparer );      Console.WriteLine( "After sorting the entire Array using the reverse case-insensitive comparer:" );      PrintIndexAndValues( myArr );   }   public static void PrintIndexAndValues( String[] myArr )  {      for ( int i = 0; i < myArr.Length; i++ )  {         Console.WriteLine( "   [{0}] : {1}", i, myArr[i] );      }      Console.WriteLine();   }}

0 0
原创粉丝点击