using
System;
using System.Collections.Generic;
namespace _IComparable
{
class Student :IComparable
{
private string name;
private int age;
public String Name
{
get { return name;}
set {name = value;}
}
public int Age
{
get { return age;}
set {age = value;}
}
public Student( string name, int age)
{
this .name = name;
this .age = age;
}
public int CompareTo( object obj)
{
if ( ! (obj is Student))
throw new ArgumentException( " ArgumentException " );
return name.CompareTo((obj as Student).Name);
}
public override string ToString()
{
return " 类型是 " + base .ToString() + " 姓名: " + name + " 年龄: " + age;
}
}
class MainClass
{
public static void Main( string [] args)
{
Student[] stuArr = new Student[ 3 ];
stuArr[ 0 ] = new Student( " zhangsan " , 3 );
stuArr[ 1 ] = new Student( " lisi " , 1 );
stuArr[ 2 ] = new Student( " wangwu " , 2 );
Array.Sort(stuArr);
foreach (Student s in stuArr)
{
Console.WriteLine(s);
}
Console.Read();
}
}
}
using System.Collections.Generic;
namespace _IComparable
{
class Student :IComparable
{
private string name;
private int age;
public String Name
{
get { return name;}
set {name = value;}
}
public int Age
{
get { return age;}
set {age = value;}
}
public Student( string name, int age)
{
this .name = name;
this .age = age;
}
public int CompareTo( object obj)
{
if ( ! (obj is Student))
throw new ArgumentException( " ArgumentException " );
return name.CompareTo((obj as Student).Name);
}
public override string ToString()
{
return " 类型是 " + base .ToString() + " 姓名: " + name + " 年龄: " + age;
}
}
class MainClass
{
public static void Main( string [] args)
{
Student[] stuArr = new Student[ 3 ];
stuArr[ 0 ] = new Student( " zhangsan " , 3 );
stuArr[ 1 ] = new Student( " lisi " , 1 );
stuArr[ 2 ] = new Student( " wangwu " , 2 );
Array.Sort(stuArr);
foreach (Student s in stuArr)
{
Console.WriteLine(s);
}
Console.Read();
}
}
}