using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ComparableTest
{
class Program
{
class Employee : IComparable<Employee>
{
private int empID;
public Employee(int empID)
{
this.empID = empID;
}
public override string ToString()
{
return empID.ToString();
}
public bool Equals(Employee other)
{
if (this.empID == other.empID)
{
return true;
}
else
{
return false;
}
}
public int CompareTo(Employee rhs)
{
return this.empID.CompareTo(rhs.empID);
}
}
static void Main(string[] args)
{
List<Employee> le = new List<Employee>();
Random random = new Random();
for (int i = 0; i < 5;i++ )
{
le.Add(new Employee(random.Next(10)+100));
}
for (int i = 0; i<le.Count; i++)
{
Console.Write(le[i].ToString()+",");
}
Console.WriteLine();
Console.WriteLine("after sort");
le.Sort();
for (int i = 0; i < le.Count; i++)
{
Console.Write(le[i].ToString() + ",");
}
Console.ReadLine();
}
}
}
C#_IComparable实例 - 对象ID进行排序
最新推荐文章于 2023-03-20 00:00:00 发布