使用IComparable和IComparer接口对集合进行排序

        许多集合类可以使用对象的默认方式进行排序,也可以用定制方法排序。ArraryList就是一个示例,它包含方法Sort(),这个方法使用时也可以不带参数,使用默认的方法进行排序,也可以传递IComparer接口,以比较对象对。

        给ArrayList填充简单类型数据时,就会进行默认的比较。对于自定义的类,必须在自定义的类中实现IComparable接口或定义一个支持IComparer接口的类类进行比较。下面用这两种方法分别实现排序。


创建一个控制台程序,添加一个自定义的Person类,继承接口IComparable,实现接口的方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CH11Ex05test
{
    public class Person : IComparable
    {
        public string name;
        public int age;

        public Person(string Name, int Age)
        {
            name = Name;
            age = Age;
        }

        public int CompareTo(object obj)
        {
            if (obj is Person)     //判断参数类型是否正确
            {
                return this.age - (obj as Person).age;  //按年龄大小排序
            }
            else
                throw new ArgumentException("the object is not person class");
        }
    }
}
再用另一种方式实现Person类的排序创建一个自定义的支持IComparer接口的PersonByName类.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace CH11Ex05test
{
    public class PersonByName : IComparer
    {
        public static IComparer Default = new PersonByName();   //在排序时需要用到<span style="font-family: Arial, Helvetica, sans-serif;">PersonByName对象调用Compare方法,定义为静态类更方便调用。</span>

        public int Compare(object x, object y)
        {
            if (x is Person && y is Person)
            {
                return Comparer.Default.Compare(((Person)x).name, ((Person)y).name);  //按名字排序
            }
            else
                throw new ArgumentException("one or both object is not person");
        }
    }
}

主程序如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace CH11Ex05test
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList List = new ArrayList();
            List.Add(new Person("person1", 10));
            List.Add(new Person("person3", 30));
            List.Add(new Person("person2", 15));
            List.Add(new Person("person4", 20));

            Console.WriteLine("not sort");
            foreach(Person obj in List)
            {
                Console.WriteLine(obj.name + "," + obj.age.ToString());
            }

            Console.WriteLine("sort by default");
            List.Sort();     //不带参数,使用默认的排序方式,即Compare类的CompareTo()方法
            foreach (Person obj in List)
            {
                Console.WriteLine(obj.name + "," + obj.age.ToString());
            }

            Console.WriteLine("sort by name");
            List.Sort(PersonByName.Default);  //传递IComparer接口,以比较对象
            foreach (Person obj in List)
            {
                Console.WriteLine(obj.name + "," + obj.age.ToString());
            }

            Console.ReadKey();
        }
    }
}

运行结果:



结论:

使用IComparable接口和IComparer接口进行排序的区别

1、IComparable接口要在自定义的类中实现,用以比较该对象和另外一个对象;

2、IComparer接口要在一个单独的类中实现,可以比较任意两个对象.




猜你喜欢

转载自blog.csdn.net/GK_2014/article/details/49029859