IComparer 与 IComparable
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的更多相关文章
- .NET Framework System.Array.Sort 数组类,加深对 IComparer、IComparable 以及泛型委托、匿名方法、Lambda 表达式的理解
本文内容 自定义类 Array.Sort 参考资料 System.Array.Sort 有很多对集合的操作,比如排序,查找,克隆等等,你可以利用这个类加深对 IComparer.IComparable ...
- CSharp - Comparison between IComparer and IComparable
/* Author: Jiangong SUN */ I've already written an article introducing the usage of comparer here. I ...
- Comparer<T> IComparer<T> IComparable<T>
Comparer<T>.Default Property Comparer<T>.Default doesn't use your FooComparer class. It ...
- Icomparer和Icomparable集合排序
c#中实现对象集合的排序可以使用ArrayList中的Sort()方法,而有比较才能谈排序,因为不是基本类型(如string ,int.double......等)所以.NET Framework不可 ...
- IComparer、IComparable、StringComparison枚举、CultureInfo 的用法
IEnumerable<T> 和 IEnumerator<T>.泛型版本是新式代码的首要选项. InvariantCulture:程序间.程序数据库.程序网络交互用Invari ...
- C#中如何使用IComparable<T>与IComparer<T>接口(转载)
本分步指南描述如何使用两个接口: IComparer和IComparable.在同一篇文章中讨论这些接口有两个原因.经常在一起,使用这些接口和接口类似 (并且有相似的名称),尽管它们用于不同用途. 如 ...
- C# 中的IComparable和IComparer
前言 在开发过程中经常会遇到比较排序的问题,比如说对集合数组的排序等情况,基本类型都提供了默认的比较算法,如string提供了按字母进行排序,而int整数则是根据整数大小进行排序.但是在引用类型中(具 ...
- 转载:C#中的泛型
泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确定一个或多个具 ...
- C#中的泛型 【转】
C#中的泛型 泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确 ...
随机推荐
- CentOS 安装 OpenResty
方案一 1.在 CentOS 系统中添加 openresty 资源库sudo yum-config-manager --add-repo https://openresty.org/yum/cn/ce ...
- SQL点点滴滴_特殊用法笔记
声明: 本文为转载,感谢原作者的辛勤付出. 原博客地址为:http://www.cnblogs.com/icyJ/p/SQL_Statement.html 1.MERGE用法:关联两表,有则改,无则加 ...
- Python 爬虫练手项目—酒店信息爬取
from bs4 import BeautifulSoup import requests import time import re url = 'http://search.qyer.com/ho ...
- Windows Server 2003 动态网站IIS设置(图)
一.安装IIS Windows Server 2003 虽说是服务器版本,但在默认情况下并没有安装IIS,要在本地浏览asp,PHP等动态网页,就必须安装IIS.在买系统盘的时候,请注意看一下 ...
- namespace 相关
#include <iostream>using namespace std;namespace first{ int var = 5;}namespace second{ double ...
- Genymotion安卓模拟器和VirtualBox虚拟机安装、配置、测试(win7_64bit)
1.概述 VirtualBox是一个优秀的虚拟机软件,它可以在电脑上提供另一个操作系统的运行环境,使多个系统同时运行.VirtualBox支持的操作系统包括Windows.Mac OS X.Linux ...
- LINUX下OA搭建
- Centos 7 iptables配置
systemctl status firewalld.service #检测是否开启了firewall systemctl stop firewalld.service #关闭firewall syt ...
- [转] Spark-Sql On YARN自动调整Executor数配置
在所有的NodeManager中,修改yarn-site.xml,为yarn.nodemanager.aux-services添加spark_shuffle值,并设置yarn.nodemanager. ...
- 【1】【MOOC】Python游戏开发入门-北京理工大学【第二部分-游戏开发之框架】
学习地址链接:http://www.icourse163.org/course/0809BIT021E-1001873001?utm_campaign=share&utm_medium=and ...