24

C# 泛型集合的自定义类型排序

 3 years ago
source link: http://www.cnblogs.com/guhuazhen/p/13972430.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

一、泛型集合List<T>排序

经sort方法之后,采用了升序的方式进行排列的。

            List<int> list = new List<int>() { 2, 4, 1, 3, 5, -2, 0, 10 };
            Console.Write("排序前...");
            foreach (var item in list)
            {
                Console.Write(item + "\t");
            }
            list.Sort();
            Console.WriteLine();
            Console.WriteLine("排序后...");
            foreach (var item in list)
            {
                Console.Write(item + "\t");
            }

2e2i63n.png!mobile

MNjMniv.png!mobile

二、对自定义类型进行排序

定义一个普通类:

public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

接下来,将定义的Person实例化,排序;

 List<Person> list = new List<Person>() {
                new Person(){Name="张叁",Id=1},
                new Person() {Name="李四",Id=4 },
                 new Person() {Name="王五",Id=2 },
            };
            list.Sort();
            foreach (var item in list)
            {
                Console.Write(item.Id);
            }

结果如下:

uyQF7fZ.png!mobile

AFnea2.png!mobile

在int类型中实现了IComparable,所以可以通过Sort()直接排序;

IzyeMzN.png!mobile

int类型是实现了IComparable这个接口的。那么如果让自定义类型Person也可以排序,那么试试实现该接口;

那么将上面的代码修改一下看看

  public class Person:IComparable
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public int CompareTo(object obj)
        {
            Person p = obj as Person;
            return this.Id.CompareTo(p.Id);
        }
    }

结果如下:

f2qYN3F.png!mobile

三、对集合按照多种不同规则进行排序

实际使用中,经常需要对集合按照多种不同规则进行排序,这就需要定义其他比较规则,可以在Compare方法中定义,该方法属于IComparer<T>泛型接口,请看下面的代码:

public class PersonNameDesc : IComparer<Person>
    {
        //存放排序器实例
        public static PersonNameDesc NameDesc = new PersonNameDesc();
        public int Compare(Person x, Person y)
        {
            return System.Collections.Comparer.Default.Compare(x.Name, y.Name);
        }
    }

Compare方法的参数为要进行比较的两个同类型对象,返回值为int类型,返回值处理规则与CompareTo方法相同。其中的Comparer.Default返回一个内置的Comparer对象,用于比较两个同类型对象。

下面用新定义的这个比较器对集合进行排序:

 //================对集合按照多种不同规则进行排序=========================
            List< Person > list = new List<Person>() {
                new Person(){Name="张叁",Id=1},
                new Person() {Name="李四",Id=4 },
                 new Person() {Name="王五",Id=2 },
                  new Person() {Name="李四",Id=6 },
                 new Person() {Name="王五",Id=3 },
            };
            list.Sort(PersonNameDesc.NameDesc);
            foreach (var item in list)
            {
                Console.Write(item.Name + "\t");
            }
            Console.Read();

结果如下:

JnQRRrF.png!mobile

四、使用linq进行排序

sort方法的一个重载是Comparison<T>类型的参数;

NjUZ3mR.png!mobile

那就看一下 Comparison<T>是怎么一回事吧:

ZzYZJfq.png!mobile

 List<Person> list = new List<Person>()
            {
                new Person(){Name="张叁",Id=1},
                new Person() {Name="李四",Id=4 },
                new Person() {Name="王五",Id=2 },
                new Person() {Name="李四",Id=6 },
                new Person() {Name="王五",Id=3 },
            };
            //匿名委托
            list.Sort((a, b) => a.Id - b.Id);
            foreach (Person p in list)
            {
                Console.WriteLine(p.Name + "\t" + p.Id);
            }
            Console.Read();

结果如下:

QvQf2yu.png!mobile

  List<Person> list = new List<Person>()
            {
                new Person(){Name="张叁",Id=1},
                new Person() {Name="李四",Id=4 },
                new Person() {Name="王五",Id=2 },
                new Person() {Name="李四",Id=6 },
                new Person() {Name="王五",Id=3 },
            };
            var l = from p in list
                    orderby p.Id descending
                    select p;
            foreach (Person p in l)
            {
                Console.WriteLine(p.Name + "\t" + p.Id);
            }
            Console.Read();

结果如下:

neuyuyI.png!mobile

今天的分享就到这里了,好久没写过博客了,加班多,太忙了,抽空复习一下基础知识;


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK