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

  • 相关阅读:
    Touchpad rocks
    KTorrent for KDE 4 已抵达
    小技巧: 怎样将 GNOME applets 添加到 Xfce 面板
    小本事:遵守令行发布 Twitter 新闻
    小才略: 如何将 Gmail 设置为默许的邮件客户端
    小伎俩: 若安在 Compiz 中设置菜单透明
    Firefox 使用手腕四则
    办理 VMware 在 2.6.24 内核下的成绩
    小手段:开启 GNOME 的窗口分组效果
    不喜爱 KDE 4.0 的新启动菜单? 换掉它!
  • 原文地址:https://www.cnblogs.com/wanghaibin/p/6507523.html
Copyright © 2020-2023  润新知