using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 类实现接口IComparable达到排序目的
{
class Program
{
static void Main(string[] args)
{
Person[] persons = new Person[]
{
new Person("David"),
new Person("Tom"),
new Person("Lisa"),
new Person("Jerry"),
new Person("Abbot"),
};
//Person是Person[]数组的元素,如果想要用Array.Sort方法对Person[]对象进行排序,必须在元素类型(Person类)中实现IComparable接口,否则执行Sort方法会报错
//即在Person类内创建一个方法CompareTo,实现两个Person对象的name字段字符串比较
Array.Sort(persons);
foreach (var item in persons)
{
Console.WriteLine(item.Name);
}
Console.ReadKey();
}
}
public cl
自定义类实集合现接口IComparable达到排序目的
于 2024-03-29 10:36:30 首次发布