数组自定义排序:IComparable和IComparer接口

  首先先说一下IComparable和IComparer的区别,前者必须在实体类中实现,后者可以单独出现在一个排序类中,即此类只包含一个compare方法。

  Array类使用快速算法对数组中的元素进行排序。sort()方法需要数组中的元素实现IComparable接口,下例演示了一个自定义的排序:

 class Program
    {
        static void Main(string[] args)
        {
            Person[] persons ={
                              new Person{firstName ="Dsldkjf",lastName="Agjkl"},
                              new Person{firstName ="gpok",lastName="gjys"},
                              new Person{firstName ="ugkdf",lastName="Agjkl"},
                              new Person{firstName ="idfkd",lastName="6dfbh"}
                              };

            foreach (var p in persons)
            {
                Console.WriteLine(p);
            }
            Console.WriteLine("---------------------------------------------");
            Array.Sort(persons);
            foreach (var p in persons)
            {
                Console.WriteLine(p);
            }
            Console.ReadKey();
        }
    }

    class Person : IComparable<Person>
    {
        public string firstName=string.Empty;
        public string lastName = string.Empty;

        public int CompareTo(Person other)
        {
            if (other == null) return 1;
            int result = string.Compare(this.lastName, other.lastName);
            if (result == 0)
            {
                result = string.Compare(this.firstName, other.firstName);
            }
            return result;
        }

        public override string ToString()
        {
            return string.Format("{0} {1}" ,firstName,lastName);
        }
    }

结果:

下面实现一个带选择功能的排序:

class Program
    {
        static void Main(string[] args)
        {
            Person[] persons ={
                              new Person{firstName ="Dsldkjf",lastName="Agjkl"},
                              new Person{firstName ="gpok",lastName="gjys"},
                              new Person{firstName ="ugkdf",lastName="Agjkl"},
                              new Person{firstName ="idfkd",lastName="6dfbh"}
                              };

            foreach (var p in persons)
            {
                Console.WriteLine(p);
            }
            Console.WriteLine("---------------------------------------------");
            Array.Sort(persons,new PersonComparer(PersonComparType.lastName));
            foreach (var p in persons)
            {
                Console.WriteLine(p);
            }
            Console.ReadKey();
        }
    }

    public struct Person
    {
        public string firstName;
        public string lastName;

        public override string ToString()
        {
            return string.Format("{0} {1}", firstName, lastName);
        }
    }
    public enum PersonComparType
    {
        firstName,
        lastName
    }

    public class PersonComparer : IComparer<Person>
    {
        private PersonComparType type;
        public PersonComparer(PersonComparType type)
        {
            this.type = type;
        }
        public int Compare(Person x, Person y)
        {
            //假如Person是一个类的话 需要进行下面判断
            //if (x ==null && y == null) return 0;
            //if (x == null) return 1;
            //if (y == null) return -1;
            switch (type)
            { 
                case PersonComparType.firstName:
                    return string.Compare(x.firstName, y.firstName);
                case PersonComparType.lastName:
                    return string.Compare(x.lastName, y.lastName);
                default:
                    throw new ArgumentException("unexpected compare type");
            }
 
        }
    }

结果:

posted @ 2014-02-26 11:56  蚂蚁拉车  阅读(546)  评论(0编辑  收藏  举报