C#高级编程(5) Array .Sort()、IComparable 、IComparer
来源:互联网 发布:三国志3优化版1.76 编辑:程序博客网 时间:2024/06/07 16:18
Array 类实现了对数组中元素的冒泡排序。Sort()方法需要数组中的元素实现 IComparable 接口。
简单类型,如 System.String 和 System.Int32 实现了 IComparable 接口,所以可以对包含这些类型的元
如果 Person 对象的排序方式与上述不同,或者不能修改在数组中用作元素的类,就可以执行
IComparer 接口。这个接口定义了方法 Compare()。IComparable 接口必须由要比较的类来执行,而
IComparer 接口独立于要比较的类。 这就是 Compare()方法定义了两个要比较的变元的原因。 其返回值
与 IComparable 接口的 CompareTo()方法类似。
简单类型,如 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
- C#高级编程(5) Array .Sort()、IComparable 、IComparer
- C# IComparable 和 IComparer 区别
- C#中IComparer和IComparable接口
- C#中IComparable和IComparer的应用
- C#学习笔记 IComparable接口 IComparable<T>接口 IComparer接口
- IComparable接口和IComparer接口(一)
- C#笔记25:比较和排序(IComparable和IComparer以及它们的泛型实现)
- 如何使用 Visual C# 中和 IComparer IComparable 接口
- 如何使用 Visual C# 中和 IComparer IComparable 接口
- 如何使用 Visual C# 中和 IComparer IComparable 接口
- Effective C# 使用IComparable和IComparer接口实现排序关系
- C#中IComparable接口和IComparer接口应用的实例
- C#中IComparable<T>和IComparer<T>接口的使用
- c# 实现IComparable、IComparer接口、Comparer类的详解
- IComparable and IComparer
- IComparable,IComparer排序实例
- IComparer IComparable比较大小
- IComparer 和 IComparable
- copyfile与movefile
- Codeforces Round #277.5 (Div. 2)C——Given Length and Sum of Digits...
- 常用jQuery数字类型的正则表达式
- [LeetCode] Pow(x, n)
- Android 中LayoutInflater的inflate函数用法详解
- C#高级编程(5) Array .Sort()、IComparable 、IComparer
- Tomcat中temp文件夹出现项目副本问题的解决方法
- Appium(Java)2.0.0以上运行时报错“java.lang.UnsupportedClassVersionError: io/appium/java_client/AppiumDriver”
- 【LeetCode】Best Time to Buy and Sell Stock III
- iOS NSOperation使用方法简介
- 任务调度
- java验证码--待抽象
- Linux网络层 ip_rcv()函数代码分析(__pskb_pull_tail)
- ubuntu 设置打开文件的默认方式