static void Main()
{
var people = new ArrayList();
people.AddRange(new ArrayList
{
new Person{ Name = "Jone",Age = },
new Person{ Name = "Tom",Age = },
new Person{ Name = "Lily",Age = },
new Person {Name = "July",Age = }
});
Console.WriteLine("↓↓↓ Old ↓↓↓");
foreach (Person person in people)
{
Console.WriteLine($"person name:{person.Name} age:{person.Age}");
}
people.Sort(); //new PersonComparer()
Console.WriteLine("↓↓↓ New ↓↓↓");
foreach (Person person in people)
{
Console.WriteLine($"person name:{person.Name} age:{person.Age}");
} Console.ReadKey();
}

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

   class  Person :IComparable
{
public string Name { get; set; } public int Age { get; set; }
public int CompareTo(object obj)
{
if (obj is Person person)
{
return this.Age - person.Age;
}
else
{
throw new ArgumentException("Object is not a Person");
}
}
}

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

 class PersonComparer:IComparer
{
public int Compare(object x, object y)
{
if (x is Person personX && y is Person personY)
{
if (Object.ReferenceEquals(x,y))
{
return ;
}
int result = String.Compare(personX.Name, personY.Name, StringComparison.Ordinal);
if (result == )
{
result = personX.Age - personY.Age;
} return result;
}
else
{
throw new ArgumentException("Object is not a Person");
}
}
}

IComparer 与 IComparable的更多相关文章

  1. .NET Framework System.Array.Sort 数组类,加深对 IComparer、IComparable 以及泛型委托、匿名方法、Lambda 表达式的理解

    本文内容 自定义类 Array.Sort 参考资料 System.Array.Sort 有很多对集合的操作,比如排序,查找,克隆等等,你可以利用这个类加深对 IComparer.IComparable ...

  2. CSharp - Comparison between IComparer and IComparable

    /* Author: Jiangong SUN */ I've already written an article introducing the usage of comparer here. I ...

  3. Comparer<T> IComparer<T> IComparable<T>

    Comparer<T>.Default Property Comparer<T>.Default doesn't use your FooComparer class. It ...

  4. Icomparer和Icomparable集合排序

    c#中实现对象集合的排序可以使用ArrayList中的Sort()方法,而有比较才能谈排序,因为不是基本类型(如string ,int.double......等)所以.NET Framework不可 ...

  5. IComparer、IComparable、StringComparison枚举、CultureInfo 的用法

    IEnumerable<T> 和 IEnumerator<T>.泛型版本是新式代码的首要选项. InvariantCulture:程序间.程序数据库.程序网络交互用Invari ...

  6. C#中如何使用IComparable<T>与IComparer<T>接口(转载)

    本分步指南描述如何使用两个接口: IComparer和IComparable.在同一篇文章中讨论这些接口有两个原因.经常在一起,使用这些接口和接口类似 (并且有相似的名称),尽管它们用于不同用途. 如 ...

  7. C# 中的IComparable和IComparer

    前言 在开发过程中经常会遇到比较排序的问题,比如说对集合数组的排序等情况,基本类型都提供了默认的比较算法,如string提供了按字母进行排序,而int整数则是根据整数大小进行排序.但是在引用类型中(具 ...

  8. 转载:C#中的泛型

    泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确定一个或多个具 ...

  9. C#中的泛型 【转】

    C#中的泛型 泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确 ...

随机推荐

  1. 从golang-gin-realworld-example-app项目学写httpapi (六)

    https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/validators.go 验证器 ...

  2. linux下pgAdmin4安装

    首先到pgAdmin4官方网站下载安装包:https://www.pgadmin.org/download/ 我下载的是3.0; 到文件所在目录执行安装命令: sudo pip install ./p ...

  3. OS考研复习笔记——操作系统的定义、目标、作用和发展的主要动力

    计算机系统由硬件和软件两部分组成.操作系统(OS,Operating System)是配置在计算机硬件上的第一层软件,是对硬件系统的首次补充. 硬件:计算机物理设备,即各种处理机存储器.输入/输出设备 ...

  4. MySql 时间处理

    纸上得来终觉浅,绝知此事要躬行 博客园 首页 新闻 新随笔 联系 管理 随笔- 490  文章- 0  评论- 65  MySql 时间处理 这里是一个使用日期函数的例子.下面的查询选择了所有记录,其 ...

  5. [翻译] VLDContextSheet

    VLDContextSheet 效果: A clone of the Pinterest iOS app context menu. 复制了Pinterest应用的菜单效果. Example Usag ...

  6. recycle bin tip

    if you have a question about recycle bin that can look the follow link; http://www.dba-oracle.com/t_ ...

  7. July 31st 2017 Week 31st Monday

    Elegance is the only beauty that never fades. 优雅是唯一不会褪色的美. Even the most beautiful apperace would be ...

  8. 利用describe( )中的count来检查数据是否缺省

    #-*- coding: utf-8 -*- #在python的pandas库中,只需要读入数据,然后使用describe()函数就可以查看数据的基本情况 import pandas as pd in ...

  9. Java 集合框架(常用数据结构)

    早在Java 2中之前,Java就提供了特设类.比如:向量(Vector).栈(Stack).字典(Dictionary).哈希表(Hashtable)这些类(数据结构)用来存储和操作对象组.虽然这些 ...

  10. JS hashMap实例详解

    链接:http://www.jb51.net/article/85111.htm JS hashMap实例详解 作者:囧侠 字体:[增加 减小] 类型:转载 时间:2016-05-26我要评论 这篇文 ...