using System;
class Employee:IComparable
{
private int Id;
private string Name;
public Employee(int id,string name)
{
this.Id=id;
this.Name=name;
}
public int CompareTo(object obj)
{
Employee temp=(Employee)obj;
if(this.Id>temp.Id)
{
return 1;
}
else
{
if(temp.Id==this.Id)
return 0;
else
return -1;
}
}
//Sample usage
public static void Main()
{
Employee[] employees=new Employee[5];
Console.WriteLine("Before Sort:\n");
for(int i=0;i<employees.Length;i++)
{
employees[i]=new Employee(5-i,"Employee#" + i);
Console.Write(employees[i].Id + ",");
}
Console.WriteLine();
Array.Sort(employees);
Console.WriteLine("After Sort:\n");
for(int i=0;i<employees.Length;i++)
{
Console.Write(employees[i].Id + ",");
}
Console.WriteLine();
}
}
|