网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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 students = new List();
            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
    {
        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 students = new List();
            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排序
本文路径:http://bjjierui.cn/article/gphgep.html

其他资讯