码迷,mamicode.com
首页 > 编程语言 > 详细

C#中List排序(Sort a list of complex types)

时间:2015-12-18 18:14:39      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

对于普通值类型(如int,string,double)的List来说,可以直接调用List<>.Sort()方法进行排序。如

            List<int> intInList = new List<int>();
            intInList.AddRange(new int[] { 1, 3, 2, 7, 5, 9, 8, 5, 6 });
            intInList.Sort();

但是如果List中存储的是复杂类型(Complex type),那么就需要我们现实的实现排序算法。常用的有一下三种方式:

1)让需要排序的类实现IComparable<T>接口。如:现在有一个Person类,我们可以让Person类实现IComparable<Person>接口。这种方式让Person类有一个默认的排序算法。

    public class Person : IComparable<Person>
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }


        public int CompareTo(Person other)
        {
            return this.ID.CompareTo(other.ID);
        }

        public override string ToString()
        {
            return string.Format("ID = {0}, FirstName = {1}, LastName = {2}",this.ID,this.FirstName,this.LastName);
        }
    }

上面的代码演示了让Person类默认按照ID大小排序。

测试代码:

        static void Main(string[] args)
        {
            List<int> intInList = new List<int>();
            intInList.AddRange(new int[] { 1, 3, 2, 7, 5, 9, 8, 5, 6 });
            intInList.Sort();


            List<Person> personInList = new List<Person>();
            personInList.AddRange(new Person[]
            {
                new Person()
                {
                    ID=2,
                    FirstName = "Robert",
                    LastName = "Stack"
                },

                new Person()
                {
                    ID =1 ,
                    FirstName = "John",
                    LastName = "Snow"
                },
                
                new Person()
                {
                    ID=3,
                    FirstName="Aya",
                    LastName="Stack"
                }
            });

            PrintAllList(personInList);
            Console.WriteLine(new string(-, 40));
            personInList.Sort();
            PrintAllList(personInList);
        }

        public static void PrintAllList(List<Person> personInList)
        {
            foreach (Person person in personInList)
            {
                Console.WriteLine(person);
            }
        }

运行结果:

ID = 2, FirstName = Robert, LastName = Stack
ID = 1, FirstName = John, LastName = Snow
ID = 3, FirstName = Aya, LastName = Stack
----------------------------------------
ID = 1, FirstName = John, LastName = Snow
ID = 2, FirstName = Robert, LastName = Stack
ID = 3, FirstName = Aya, LastName = Stack
Press any key to continue . . .

 

2)有的时候,我们并不能获取到Person类的源代码(如:我们正在使用一个别人写好的类库),这个时候如果我们想对Person类实现排序,可以另外写一个专门的对Person进行排序的类,让该类实现IComparer<Person>接口,然后在List<Person>.Sort()方法中传入一个该类的实例即可。代码如下:

    public class PersonComparer : IComparer<Person>
    {
        public int Compare(Person x, Person y)
        {
            return x.FirstName.CompareTo(y.FirstName);
        }
    }

上面的代码实现了对Person使用FirstName进行排序。

测试代码:

            //省略相同代码....
            PrintAllList(personInList);
            Console.WriteLine(new string(-, 40));
            personInList.Sort(new PersonComparer());
            PrintAllList(personInList);

运行结果:

ID = 2, FirstName = Robert, LastName = Stack
ID = 1, FirstName = John, LastName = Snow
ID = 3, FirstName = Aya, LastName = Stack
----------------------------------------
ID = 3, FirstName = Aya, LastName = Stack
ID = 1, FirstName = John, LastName = Snow
ID = 2, FirstName = Robert, LastName = Stack
Press any key to continue . . .

3)上例通过创建一个类,然后实现IComparer接口的方式对Person指定排序算法。当List<>.Sort()在调用的时候,实际上在其内部是调用了IComparer的方法Compare()。如果我们不想新建一个类,我们完全可以直接传一个代理函数到List<>.Sort()方法里面去,代码如下:

            //省略相同代码....
            PrintAllList(personInList);
            Console.WriteLine(new string(-, 40));
            personInList.Sort((x, y) => { return x.LastName.CompareTo(y.LastName); });
            PrintAllList(personInList);

上面的代码通过传入delegate 的方式实现了对Person类使用LastName进行排序的功能。

运行结果:

ID = 2, FirstName = Robert, LastName = Stack
ID = 1, FirstName = John, LastName = Snow
ID = 3, FirstName = Aya, LastName = Stack
----------------------------------------
ID = 1, FirstName = John, LastName = Snow
ID = 3, FirstName = Aya, LastName = Stack
ID = 2, FirstName = Robert, LastName = Stack
Press any key to continue . . .

 

C#中List排序(Sort a list of complex types)

标签:

原文地址:http://www.cnblogs.com/kuillldan/p/5057766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!