namespace Ch11Ex05
{
    class Person : IComparable
    {
        public string Name;
        public int Age;        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }        public int CompareTo(object obj)
        {
            if (obj is Person)
            {
                Person otherPerson = obj as Person;
        return this.Age - otherPerson.Age; 
            }
            else
            {
                throw new ArgumentException(
                   "Object to compare to is not a Person object.");
            }
        }
    }
}namespace Ch11Ex05
{
    public class PersonComparerName : IComparer
    {
        public static IComparer Default = new PersonComparerName();//请高人解释下这行是如何实现的?书上说这是一个公共的静态字段        public int Compare(object x, object y)
        {
            if (x is Person && y is Person)
            {
                return Comparer.Default.Compare(           //这里有点困惑,不是说用Comparer.default是啥意思
                   ((Person)x).Name, ((Person)y).Name);
            }
            else
            {
                throw new ArgumentException(
                   "One or both objects to compare are not Person objects.");
            }
        }
    }
}
namespace Ch11Ex05
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            list.Add(new Person("Jim", 30));
            list.Add(new Person("Bob", 25));
            list.Add(new Person("Bert", 27));
            list.Add(new Person("Ernie", 22));            Console.WriteLine("Unsorted people:");
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine("{0} ({1})",
                   (list[i] as Person).Name, (list[i] as Person).Age);
            }
            Console.WriteLine();            Console.WriteLine(
               "People sorted with default comparer (by age):");
            list.Sort();
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine("{0} ({1})",
                   (list[i] as Person).Name, (list[i] as Person).Age);
            }
            Console.WriteLine();            Console.WriteLine(
               "People sorted with nondefault comparer (by name):");
            list.Sort(PersonComparerName.Default);
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine("{0} ({1})",
                   (list[i] as Person).Name, (list[i] as Person).Age);
            }            Console.ReadKey();
        }
    }
}

解决方案 »

  1.   

    IComparer是接口,Default是个对象类型的公共字段 
    定义了一个静态的公共字段,一个继承于IComparer接口的类PersonComparerName的实例
      

  2.   

    路过,顶,写的真长啊。
    public static IComparer Default = new PersonComparerName();//请高人解释下这行是如何实现的?书上说这是一个公共的静态字段
    Default是多态实现,实际上是实现了IComparer的一个PersonComparerName类的对象
    return Comparer.Default.Compare( //这里有点困惑,不是说用Comparer.default是啥意思
      ((Person)x).Name, ((Person)y).Name);
    递归调用本身方法,类型相同,判断名字是否相同geren lijie