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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值