zoukankan      html  css  js  c++  java
  • IComparer 与 IComparable

     1         static void Main()
     2         {
     3             var people = new ArrayList();
     4             people.AddRange(new ArrayList
     5             {
     6                 new Person{ Name = "Jone",Age = 18},
     7                 new Person{ Name = "Tom",Age = 20},
     8                 new Person{ Name = "Lily",Age = 15},
     9                 new Person {Name = "July",Age = 25 }
    10             });
    11             Console.WriteLine("↓↓↓   Old   ↓↓↓");
    12             foreach (Person person in people)
    13             {
    14                 Console.WriteLine($"person name:{person.Name} age:{person.Age}");
    15             }
    16             people.Sort(); //new PersonComparer()
    17             Console.WriteLine("↓↓↓   New   ↓↓↓");
    18             foreach (Person person in people)
    19             {
    20                 Console.WriteLine($"person name:{person.Name} age:{person.Age}");
    21             }
    22 
    23             Console.ReadKey();
    24         }
    View Code

    IComparable 是用于类对其的CompareTo方法进行实现,比较的对象包含自身,并且集合调用默认的Sort方法 (个人理解是框架提供的默认比较方式)

     1   class  Person :IComparable
     2     {
     3         public string Name { get; set; }
     4 
     5         public int Age { get; set; }
     6         public int CompareTo(object obj)
     7         {
     8             if (obj is Person person)
     9             {
    10                 return this.Age - person.Age;
    11             }
    12             else
    13             {
    14                 throw new ArgumentException("Object is not a Person");
    15             }
    16         }
    17     }
    View Code

    IComparer  是比较两个Object,是以比较器的形式存在的,并且集合调用Sort(new XxxComparer())方法(个人理解是框架对默认比较方式的扩展吧,用以满足特定的需求)

     1 class PersonComparer:IComparer
     2     {
     3         public int Compare(object x, object y)
     4         {
     5             if (x is Person personX && y is Person personY)
     6             {
     7                 if (Object.ReferenceEquals(x,y))
     8                 {
     9                     return 0;
    10                 }
    11                 int result = String.Compare(personX.Name, personY.Name, StringComparison.Ordinal);
    12                 if (result == 0)
    13                 {
    14                     result = personX.Age - personY.Age;
    15                 }
    16 
    17                 return result;
    18             }
    19             else
    20             {
    21                 throw new ArgumentException("Object is not a Person");
    22             }
    23         }
    24     }
    View Code
  • 相关阅读:
    http基本概念和常见状态码
    JDK1.7 hashMap并发扩容死循环原理
    HashMap初始化数组原理,为什么是2的N次方?
    Rest接口加Http单向认证
    post和get乱码解决方法
    用迭代法实现二叉树的前、中、后序遍历
    搭建基于Zookeeper和solr的分布式搜索:solrcloud
    正向代理,反向代理及其作用
    二分查找向左或向右紧缩
    tcp四次挥手
  • 原文地址:https://www.cnblogs.com/bhfdz/p/8984006.html
Copyright © 2011-2022 走看看