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

  • 相关阅读:
    将数据加载时显示的图片和文字提成公共的--实现方法
    JavaScript【面向对象】-静态方法-私有方法-公有方法-特权方法
    强大的JS数组
    sql工作问题总结
    JSON.stringify()的使用--将string转换成json
    JS小技巧
    JS工作积累
    (六)Redis之数据结构之Set
    (五)Redis之List
    (四)Redis之哈希
  • 原文地址:https://www.cnblogs.com/wanghaibin/p/6507523.html
Copyright © 2020-2023  润新知