首页 > 编程知识 正文

如何使用sort排序,list排序sort默认排序

时间:2023-05-05 18:55:23 阅读:151199 作者:4938

公共void sort (; 公共语音软件(comparisontcomparison ); 公共void sort (icomparertcomparer; 公共void sort (intindex,int count,IComparerT comparer ); 四种方法介绍

公共void sort (; 一个是无参数的,但list类型存在限制,list中的元素对象必须继承IComparable接口。 此外,IComparable接口中的compare to (compare to )方法必须自己实现对象的比较规则才能实现该方法。

例如,Int32和Double都是实现IComparable接口并重载CompareTo方法的结构。 (注: int和double均为Int32和double的别名(alias ) )

public void sort (comparison comparison ) ) ) ) ) ) ) )。

有比较代理方法的参数,内部参数可以是委托也可以是方法

一种叫public void sort (icomparercomparer )的方法可以定制自己的比较方法

公共void sort (intindex,int count,IComparer comparer ); 这个方法实际上必须补充第二个方法,可以从第几个要素开始重新排列几个要素

using System.Collections; using system.collections.generic; using system.text.regular expressions; using UnityEngine; public class cmd : mono behaviour { void start (} { liststudentobjs=new list student ) }{newstudent ) 10},newstudent ) 330 //第二种方法//此处还可以委托objs.sort(delegate(studenta,student b ) /第二种方法(returna.age.compareto ) b.age ) ); objs.sort(newstudentcompare ) );//第三种方法objs.sort (1,4,new studentCompare ) ); //第四种方法(intcompare(studenta,student b ) ) /排序规则(returna.age.compareto ) b.age ); //在学生班student中比较年龄}}public class student//学生班{ private int age; 公共int age { get=age; set=age=value; }publicstudent(int_age ) { age=_age; }//个比较器为icomparerpublicclassstudentcompare : icomparerstudent { public int compare (studentx,student y ) returnx.agg

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。