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

实现比较器接口IComparable<T>,让自定义类型数组也能排序

时间:2017-03-06 01:30:25      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:数组   style   .so   string   using   enum   pac   last   logs   

 

using System;

namespace LinqDemo1
{
    class Program
    {
        static void Main(string[] args)
        {
            Person[] persons =
            {
                new Person {FirstName = "Damon", LastName = "Hill"},
                new Person {FirstName = "Niki", LastName = "Lauda"},
                new Person {FirstName = "Ayrton", LastName = "Senna"}
            };
            Array.Sort(persons);//现在可以比较了
            foreach (var p in persons)
            {
                Console.WriteLine(p);
            }

            Console.ReadKey();
        }
    }

    class Person : IComparable<Person>
    {
        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);
        }

        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

 

但有时候Person类是别人的类,无法修改,那怎么办呢?

在这种情况下,就可以定义一个新类,如:PersonComparer继承IComparer<Person>

using System;
using System.Collections;
using System.Collections.Generic;

namespace LinqDemo1
{
    class Program
    {
        static void Main(string[] args)
        {
            Person[] persons =
            {
                new Person {FirstName = "Damon", LastName = "Hill"},
                new Person {FirstName = "Niki", LastName = "Lauda"},
                new Person {FirstName = "Ayrton", LastName = "Senna"}
            };
            Array.Sort(persons, new PersonComparer(PersonCompareType.FirstName));

            foreach (var p in persons)
            {
                Console.WriteLine(p);
            }

            Console.ReadKey();
        }
    }

    public enum PersonCompareType
    {
        FirstName,
        LastName
    }

    public class PersonComparer : IComparer<Person>
    {
        private PersonCompareType compareType;

        public PersonComparer(PersonCompareType compareType)
        {
            this.compareType = compareType;
        }

        public int Compare(Person x, Person y)
        {
            if (x == null && y == null) return 0;
            if (x == null) return 1;
            if (y == null) return -1;

            switch (compareType)
            {
                case PersonCompareType.FirstName:
                    return string.Compare(x.FirstName, y.FirstName);
                case PersonCompareType.LastName:
                    return string.Compare(x.LastName, y.LastName);
                default:
                    throw new ArgumentException("unexpected compare type");
            }

        }
    }

    public class Person
    {
        public override string ToString()
        {
            return string.Format("{0} {1}", FirstName, LastName);
        }

        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

 Array 类还提供了Sort()多个重载方法,它需要将一个委托作为参数,这个参数可以传递给方法,从而比较2个对象,而不需要依赖IComparable或IComparer接口,也就是说上面都白折腾了,有更高级的做法!那就是委托!

 

实现比较器接口IComparable<T>,让自定义类型数组也能排序

标签:数组   style   .so   string   using   enum   pac   last   logs   

原文地址:http://www.cnblogs.com/wanghaibin/p/6507523.html

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