阅读背景:

Icomparable与Icomparer实现显示接口以显示接口排序为准

来源:互联网 
实现
class Preson : IComparable
    {   
        public string Name { get; set; }
        public int Age { get; set; }

        public int CompareTo(object other)
        {
            Preson otherz = other as Preson;
            return Age.CompareTo(otherz.Age);
        }
        int IComparable.CompareTo(object obj)
        {
            Preson otherz = obj as Preson;
            return Name.CompareTo(otherz.Name);
        }



        public override string ToString()
        {
            return Name + "----" + Age.ToString() + "\r\n";
        }
    }


 static void Main(string[] args)
 {
     ArrayList studentList = new ArrayList();
     studentList.Add(new Preson() { Age = 1, Name = "a1" });
     studentList.Add(new Preson() { Age = 5, Name = "g1" });
      studentList.Add(new Preson() { Age = 4, Name = "b1" });
      studentList.Add(new Preson() { Age = 2, Name = "f1" });
     studentList.Sort();
     foreach (Preson item in studentList)
     {
      Console.Write(item.ToString());
     }
 }
class Preson : IComparable
    {   
   



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: