ICompare接口、Sort()方法

1、接口中的方法并不实现具体功能,继承于该接口的类必须实现具体功能。

核心思想:对修改封闭,对扩展开放。

2、Sort()方法:

默认:Sort(内容)

英文:按照首字母顺序排序,首字母相同则看第二个字母;

数字:从小到大排序;

自定义 Sort(内容,排序规则)

内容按照自定义的规则排序。

host类

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)
{
// 创建一个数组,里面放汽车名和年份
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("排序前\n");
foreach (car c in arrayOfCars)
Console.WriteLine(c.Make + "\t\t" + c.Year); Console.WriteLine("\Sort默认正向排序 \n");
Array.Sort(arrayOfCars);
foreach (car c in arrayOfCars)
Console.WriteLine(c.Make + "\t\t" + c.Year);
 Console.WriteLine("\Sort 按年份正向排序 )\n");
Array.Sort(arrayOfCars, car.sortYearAscending());
foreach (car c in arrayOfCars)
Console.WriteLine(c.Make + "\t\t" + c.Year); Console.WriteLine("\Sort 按汽车名正向排序 \n");
Array.Sort(arrayOfCars, car.sortMakeDescending());
foreach (car c in arrayOfCars)
Console.WriteLine(c.Make + "\t\t" + c.Year); Console.WriteLine("\Sort 按汽车名反向排序 \n");
Array.Sort(arrayOfCars, car.sortYearDescending());
foreach (car c in arrayOfCars)
Console.WriteLine(c.Make + "\t\t" + c.Year); Console.ReadLine();
}
}
}

  

Car类

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接口来提供默认的排序顺序的对比。
public int IComparable.CompareTo(object obj)
{
car c = (car)obj;
        //调用了C#中封装的类,详情见下一篇
return String.Compare(this.make, c.make);
}      //因为这里使用了嵌套类,把嵌套类包裹到方法中方便调用(类名.方法名)
public static IComparer sortYearAscending()
{
        //调用了上面的类
return (IComparer)new sortYearAscendingHelper();
} public static IComparer sortYearDescending()
{
        //调用了上面的类
return (IComparer)new sortYearDescendingHelper();
} public static IComparer sortMakeDescending()
{
        //调用了上面的类
return (IComparer)new sortMakeDescendingHelper();
} }
}

  

上一篇:maven 中snapshot版本和release版本的区别


下一篇:RESTful Web Services测试工具推荐