Code
//
public interface Shape
{
void Draw();
}
public class Circle:Shape
{
public void Draw()
{Console.WriteLine("Draw Circle");}
}
public class Square:Shape
{
public void Draw()
{Console.WriteLine("Draw Square");}
}
public class MyComparer:IComparer
{
private Hashtable typelist = new Hashtable();
public void Add(Type type,int order)
{ typelist.Add(type, order); }
#region IComparer 成员
public int Compare(object x, object y)
{
int i1 = (int)typelist[x.GetType()];
int i2 = (int)typelist[y.GetType()];
//if it store the value type ,it don't implement the interface IComparable
return i1.CompareTo(i2);
}
#endregion
}
public class App
{
static void Main()
{
MyComparer compare = new MyComparer();
compare.Add(typeof(Circle), 1);
compare.Add(typeof(Square), 2);
ArrayList list = new ArrayList();
list.Add(new Square());
list.Add(new Circle());
list.Add(new Square());
list.Sort(compare);