staticvoid Main (string[] args){//传入一个学生集合
Stu[] s =newStu[]{newStu{ ID =1,Name ="张三",Age =19},newStu{ ID =2,Name ="李四",Age =20},newStu{ ID =3,Name ="王五",Age =18}};//调用泛型冒泡排序var n =sort<Stu>(s);//遍历输出排序后的学生姓名foreach(var item in n){
Console.WriteLine(item.Name);}
Console.ReadKey();}//泛型数组 冒泡排序static T[]sort<T>(T[] x)where T:IComparable<T>{T b =default(T);for(int i =0; i < x.Length-1; i++){for(int j =0; j < x.Length-i-1; j++){if(x[j].CompareTo(x[j+1])<0){
b = x[j];
x[j]= x[j +1];
x[j +1]= b;}}}return x;}//继承接口publicclassStu:IComparable<Stu>{publicint ID {get;set;}publicstring Name {get;set;}publicint Age {get;set;}publicintCompareTo(Stu other){//根据年龄来排序returnthis.Age.CompareTo(other.Age);}}
posted on
2019-11-06 20:07.net之路
阅读(71)
评论(0)
编辑收藏举报