C# 排序ICompare<>接口的使用
IComparer<T> 这是一个接口,是用来给对象中的某一个属性经行排序
1 class NameDESC:IComparer<StudentExt> 2 { 3 public int Compare(StudentExt x,StudentExt y) 4 { 5 return y.StudentName.CompareTo(x.StudentName); 6 } 7 } 8 //按照学号降序排列 9 class StudentIdDESC:ICompaner<StudentExt> 10 { 11 public int Compare(StudentExt x,StudentExt y) 12 { 13 return y.StudentId.CompareTo(x.StudentId); //因为是降序所以是y.CompareTo 14 } 15 } 16 //点击插叙进行排序 17 private void btnStuIdDESC_Click(object sender,EventArgs e) 18 { 19 this.list.Sort(new StudentIdDESC()); 20 this.dgvStudentList.DataSource=null; 21 thid.dgvStudentList.DataSource=list; 22 }