c#中listsort排序

1、List.Sort (泛型 Comparison) 法

成都创新互联公司长期为超过千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为安岳企业提供专业的网站设计、网站建设安岳网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。

 

此方法的参数是Comparison类型,其实是一个包含两个参数的委托,因此使用此方法,我们只需要定义一个委托,其两个参数均为Student类型,在委托实现的方法比较两个Student对象的Age属性即可。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericCompare
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> students = new List<Student>();
            students.Add(new Student("001","kenshincui",25));
            students.Add(new Student("002", "miaoer", 23));
            students.Add(new Student("003", "shenjinjuan", 22));
            students.Add(new Student("004", "nieyanxin", 24));
            Console.WriteLine("未进行排序之前:");
            foreach (Student st in students)
            {
                Console.WriteLine(st.No+","+st.Name+","+st.Age+";");
            }
            Console.WriteLine("List.Sort (泛型 Comparison) 排序之后:");
            students.Sort(delegate(Student a, Student b) { return a.Age.CompareTo(b.Age); });
            foreach (Student st in students)
            {
                Console.WriteLine(st.No + "," + st.Name + "," + st.Age + ";");
            }
            Console.ReadKey();
        }
    }
}

2、List.Sort (泛型 IComparer)

此方法需要一个泛型IComparer接口类型,因此只要定义一个类实现此接口然后再调用此方法即可。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericCompare
{
    class StudentCompare :IComparer<Student>
    {
        public int Compare(Student a, Student b)
        {
            return a.Age.CompareTo(b.Age);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericCompare
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> students = new List<Student>();
            students.Add(new Student("001","kenshincui",25));
            students.Add(new Student("002", "miaoer", 23));
            students.Add(new Student("003", "shenjinjuan", 22));
            students.Add(new Student("004", "nieyanxin", 24));
            Console.WriteLine("未进行排序之前:");
            foreach (Student st in students)
            {
                Console.WriteLine(st.No+","+st.Name+","+st.Age+";");
            }
            Console.WriteLine("List.Sort (泛型 IComparer) 排序之后:");
            students.Sort(new StudentCompare());
            foreach (Student st in students)
            {
                Console.WriteLine(st.No + "," + st.Name + "," + st.Age + ";");
            }
            Console.ReadKey();
        }
    }
}

参考资料:  c#中list排序    http://www.studyofnet.com/news/531.html

名称栏目:c#中listsort排序
网站网址:https://www.cdcxhl.com/article48/gphgep.html

成都网站建设公司_创新互联,为您提供品牌网站设计ChatGPT营销型网站建设服务器托管软件开发网站改版

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设