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

 

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接口,也就是说上面都白折腾了,有更高级的做法!那就是委托!

 

转载于:https://www.cnblogs.com/wanghaibin/p/6507523.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值