C#中实现接口排序 .

来源:互联网 发布:centos 6.5 搭建lamp 编辑:程序博客网 时间:2024/05/16 07:44
 
  1. IComparable接口和IComparable<T>接口【实现两个对象之间的比较】  
  2.   
  3.   
  4.     接口将会实现CompareTo(Object obj)和CompareTo(Student student)  
  5.     代码如下:  
  6.         public int CompareTo(object obj)  
  7.             {  
  8.               throw new NotImplementedException();  
  9.             }  
  10.     案例:  
  11.         class Student:IComparable<Student>  
  12.         {  
  13.              public string Name { getset; }  
  14.   
  15.                  public int Age { getset; }  
  16.   
  17.                  public string Address { getset; }  
  18.   
  19.              //该方法实现年龄的比较   
  20.              public int CompareTo(Student other)  
  21.                  {  
  22.                     return this.Age.CompareTo(other.Age);  
  23.                  }  
  24.       
  25.         }  
  26.   
  27.         class Program  
  28.         {  
  29.             static void Main(string[] args)  
  30.             {  
  31.                 Student stu1=new Student(){Name="zhangsan",Age=18,Address="xiangfan"};  
  32.                 Student stu2=new Student(){Name="lisi",Age=20,Address="wuhan"};  
  33.                 int result=stu1.CompareTo(stu2);  
  34.                 结果:  
  35.                 如果result>0   stu1.Age > stu2.Age  
  36.                 如果result=0  stu1.Age = stu2.Age  
  37.                 如果result<0   stu1.Age < stu2.Age  
  38.             }  
  39.         }  
  40.   
  41.   
  42. IComparer接口和IComparer<T>接口【可实现集合对象按照对象的某一个属性进行的排序】  
  43.   
  44.     接口将实现Compare(Object x,Object y)、Compare(Student x, Student y)  
  45.   
  46.   
  47.     //实现Student类中以年龄进行排序   
  48.     class   ComparerStudentAge:IComparer<Student>  
  49.     {  
  50.         public int Compare(Student x,Student y)           
  51.         {  
  52.             return x.Age.CompareTo(y.Age);  
  53.         }  
  54.     }  
  55.       
  56.     //实现Student类中以姓名进行排序   
  57.     class ComparerStudentName:IComparer<Student>  
  58.     {  
  59.         public int Compare(Student x,Student y)  
  60.         {  
  61.             return x.Name.CompareTo(y.Name);  
  62.         }  
  63.     }  
  64.   
  65.     class Program  
  66.     {  
  67.         static void Main(string[] args)  
  68.         {  
  69.              List<Student> list = new List<Student>()   
  70.                      {  
  71.                         new Student(){ Name="zhangsan", Age=18, Address="上海浦东"},  
  72.                         new Student(){Name="lisi",Age=20,Address="上海闸北"},  
  73.                         new Student(){Name="wangwu",Age=22,Address="襄樊"},  
  74.                         new Student(){Name="zhaoliu",Age=15,Address="武汉"},  
  75.                         new Student(){Name="qianqi",Age=39,Address="随州"},  
  76.                         new Student(){Name="kunkun",Age=21,Address="襄樊"}  
  77.                      };  
  78.   
  79.             //使用姓名排序       
  80.             list.Sort(new ComparerStudentName());  
  81.             //使用年龄进行排序   
  82.             list.Sort(New ComparerStudentAge());  
  83.         }  
  84.     }  
原创粉丝点击