Public class student: icomparable <student>
{
Private string name;
Private int age;
Public student (string _ name, int _ age)
{
Name = _ name;
Age = _ age;
}
Public string name
{
Get
{
Return this. Name;
}
Set
{
This. Name = value;
}
}
Public int age
{
Get
{
Return this. Age;
}
Set
{
This. Age = value;
}
}
Public override string tostring ()
{
Return string. Format ("{0}, {1}", this. Name, this. Age );
}
# Region icomparable Member
Public int compareto (student other)
{
Return this. Age. compareto (other. Age );
}
# Endregion
}
// Calls in main
Student son = new student ("son", 23 );
Student Tom = new student ("Tom", 45 );
Student anle = new student ("anle", 32 );
List <student> List = new list <student> ();
List. Add (son );
List. Add (Tom );
List. Add (anle );
Console. writeline ("----- before sorting -----");
Foreach (student s in List)
{
Console. writeline (s );
}
List. Sort ();
Console. writeline ("----- after sorting -----");
Foreach (student s in List)
{
Console. writeline (s );
}