IComparable<>这个接口使类可以被比较
public class Person: IComparable<Person>{
public string name;
public int num;
public int num1;
public Person(string name, int num,int num1){
this.name = name;
this.num = num;
this.num1 = num1;
}
public int CompareTo(Person other){
if (other.num == 10)
return -1;
if (this.num < other.num)
return 1;
if (this.num == other.num)
return 0;
if (this.num > other.num)
return -1;
return 0;
}
}
IComparer<>这个接口可以另外的类变成比较器,<>里填的是要比较的类,同时可以控制要排序的字段
public class com: IComparer<Person>{
private int type;
public com(int type){
this.type = type;
}
public int Compare(Person x, Person y){
switch (this.type) {
case 0: return x.num.CompareTo(y.num); break;
case 1: return x.num1.CompareTo(y.num1); break;
default: throw new ArgumentException("0000");
}
}
}```