C#_IComparableインスタンス-オブジェクトIDのソート
1713 ワード
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();
}
}
}