C#比较器Comparer/Sort排序的类内实现和类外实现
比较规则
CompareTo(Object)
将此实例与指定对象进行比较并返回一个对二者的相对值的指示。
public int CompareTo (object? value);
参数value Object要比较的对象,或为 null。
返回Int32一个带符号数字,指示此实例和 value 的相对值。
返回值 |
说明 |
小于零 |
此实例小于 value。 |
零 |
此实例等于 value。 |
大于零 |
此实例大于 value,或 value 为 null。 |
类内实现比较器
//Person类,自带比较器
public class Person : IEquatable<Person>, IComparable<Person>
{
public int age;
public string name;
public Person(int age)
{
this.age = age;
}
public Person(string name, int age) : this(age)
{
this.name = name;
}
public override string ToString()
{
return $"姓名:{this.name} + 年龄: {this.age}";
}
//实现可比较接口
public int CompareTo(Person other)
{
if (other == null) return 1;
return this.age.CompareTo(other.age);
}
//实现可相等接口
public bool Equals(Person other)
{
if (other == null) return false;
return this.age.Equals(other.age);
}
//运算符重载
public static bool operator >(Person person, Person other)
{ if (other == null) return true; return person.age > other.age; }
public static bool operator >=(Person person, Person other)
{ if (other == null) return true; return person.age >= other.age; }
public static bool operator <(Person person, Person other)
{ if (other == null) return false; return person.age < other.age; }
public static bool operator <=(Person person, Person other)
{ if (other == null) return false; return person.age <= other.age; }
}
测试
static void test1()
{
List<Person> list = new List<Person>();
Random r = new Random();
for (int i = 0; i < 10; i++)
{ list.Add(new Person($"Tom{i}", r.Next(1, 100))); }
foreach (var item in list)
{ Console.WriteLine(item.ToString()); }
//Person自带比较器
list.Sort();
Console.WriteLine("排序后");
foreach (var item in list)
{ Console.WriteLine(item.ToString()); }
}
运行结果
类外实现
//学生类,无比较器,需要在外部专门声明一个比较器
public class Student
{
public int age;
public string name;
public Student(int age)
{
this.age = age;
}
public Student(string name, int age) : this(age)
{
this.name = name;
}
public override string ToString()
{
return $"姓名:{this.name} + 年龄: {this.age}";
}
}
//学生比较器,实现可比较接口(默认)
public class StudentComparerAscend : IComparer<Student>
{
public int Compare(Student x, Student y)
{
int result;
if(x==null&&y==null ) result= 0;
if (x == null && y != null) result = -1;
if (x != null && y == null) result = 1;
result= x.age.CompareTo(y.age);
return result;
}
}
//学生比较器,实现可比较接口(反序)
public class StudentComparerDescend : IComparer<Student>
{
public int Compare(Student x, Student y)
{
int result;
if (x == null && y == null) result = 0;
if (x == null && y != null) result = -1;
if (x != null && y == null) result = 1;
result = x.age.CompareTo(y.age);
return -result;//实现反序
}
}
测试
static void test2()
{
List<Student> list = new List<Student>();
Random r = new Random();
for (int i = 0; i < 10; i++)
{ list.Add(new Student($"Jack{i}", r.Next(1, 100))); }
foreach (var item in list)
{ Console.WriteLine(item.ToString()); }
//实例化升序比较器
StudentComparerAscend comparerAscend = new StudentComparerAscend();
//升序排序(默认)
list.Sort(comparerAscend);
Console.WriteLine("\n升序排序后");
foreach (var item in list)
{ Console.WriteLine(item.ToString()); }
//实例化降序比较器
StudentComparerDescend comparerDescend = new StudentComparerDescend();
//降序排序
list.Sort(comparerDescend);
Console.WriteLine("\n降序排序后");
foreach (var item in list)
{ Console.WriteLine(item.ToString()); }
}
运行结果