using System; using System.Collections; namespace ConsoleEnum { public class car : IComparable { // 嵌套类的开始。 // 按年升序 private class sortYearAscendingHelper : IComparer { //实现接口定义的方法 int IComparer.Compare(object a, object b) { car c1 = (car)a; car c2 = (car)b; if (c1.year > c2.year) return 1; if (c1.year < c2.year) return -1; else return 0; } } // 按年降序 private class sortYearDescendingHelper : IComparer { int IComparer.Compare(object a, object b) { car c1 = (car)a; car c2 = (car)b; if (c1.year < c2.year) return 1; if (c1.year > c2.year) return -1; else return 0; } } // 按制造商字母降序 private class sortMakeDescendingHelper : IComparer { int IComparer.Compare(object a, object b) { car c1 = (car)a; car c2 = (car)b; return String.Compare(c2.make, c1.make); } } // 嵌套类结束 private int year; private string make; //构造函数 public car(string Make, int Year) { make = Make; year = Year; } //属性 public int Year { get { return year; } set { year = value; } } public string Make { get { return make; } set { make = value; } } // 实现IComparable接口来提供默认的排序顺序的对比。 // 将当前实例与同一类型的另一个对象进行比较,并返回一个整数,该整数指示当前实例在排序顺序中的位置是位于另一个对象之前、之后还是与其位置相同。 int IComparable.CompareTo(object obj) { car c = (car)obj; return String.Compare(this.make, c.make); } // 返回排序辅助IComparer对象的方法。 public static IComparer sortYearAscending() { return (IComparer)new sortYearAscendingHelper(); } // 返回排序辅助IComparer对象的方法。 public static IComparer sortYearDescending() { return (IComparer)new sortYearDescendingHelper(); } // 返回排序辅助IComparer对象的方法。 public static IComparer sortMakeDescending() { return (IComparer)new sortMakeDescendingHelper(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleEnum { class host { [STAThread] static void Main(string[] args) { // Create an arary of car objects. car[] arrayOfCars = new car[6] { new car("Ford",1992), new car("Fiat",1988), new car("Buick",1932), new car("Ford",1932), new car("Dodge",1999), new car("Honda",1977) }; Console.WriteLine("Array - Unsorted\n"); foreach (car c in arrayOfCars) Console.WriteLine(c.Make + "\t\t" + c.Year); // 演示时用“默认”排序的数组排序。 Array.Sort(arrayOfCars); Console.WriteLine("\nArray - Sorted by Make (Ascending - IComparable)\n"); foreach (car c in arrayOfCars) Console.WriteLine(c.Make + "\t\t" + c.Year); // Demo ascending sort of numeric value with IComparer. //要排序的数组,自定义的排序规则 Array.Sort(arrayOfCars, car.sortYearAscending()); Console.WriteLine("\nArray - Sorted by Year (Ascending - IComparer)\n"); foreach (car c in arrayOfCars) Console.WriteLine(c.Make + "\t\t" + c.Year); // Demo descending sort of string value with IComparer. Array.Sort(arrayOfCars, car.sortMakeDescending()); Console.WriteLine("\nArray - Sorted by Make (Descending - IComparer)\n"); foreach (car c in arrayOfCars) Console.WriteLine(c.Make + "\t\t" + c.Year); // Demo descending sort of numeric value using IComparer. Array.Sort(arrayOfCars, car.sortYearDescending()); Console.WriteLine("\nArray - Sorted by Year (Descending - IComparer)\n"); foreach (car c in arrayOfCars) Console.WriteLine(c.Make + "\t\t" + c.Year); Console.ReadLine(); } } }
时间: 2024-11-04 05:29:57