IComparable和IComparer接口
C#中,自定义类型,支持比较和排序,需要实现IComparable接口。IComparable接口存在一个名为CompareTo()的方法,接收类型为object的参数表示被比较对象,返回整型值:1表示当前对象大于被比较对象,0表示两者相等,-1表示当前对象小于被比较对象。
public int CompareTo(object o) {}
若想以更加灵活的方式对自定义类进行比较,可以声明一个继承自IComparer接口的比较器,实现接口方法Comprae(),接收2个object类型参数作为比较对象,返回整型值:1表示前者大于后者,0表示两者相等,-1表示前者小于后者。
public int Compare(object x, object y) {}
IComparable是“可比较的”意思,自定义类实现该接口,就具有可比较的功能;IComparer是“比较器”的意思,实现该接口的类就是一个比较器,可以将比较器“注入”类中,使类具有比较和排序的功能。
IComparable接口示例代码
定义学生类,该类实现IComparable接口的CompareTo方法,该方法对Age 进行大小比较。
public class Student : IComparable
{
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
public int CompareTo(object obj)
{
Student stu = obj as Student;
if (Age > stu.Age)
{
return ;
}
else if (Age == stu.Age)
{
return ;
}
else
{
return -;
}
}
}
调用List<T>.Sort方法实现stuList按照学生的年龄来排序。
static void Main(string[] args)
{
List<Student> stuList = new List<Student>();
stuList.Add(new Student() { Name = "tiana0", Sex = "Man", Age = });
stuList.Add(new Student() { Name = "tiana1", Sex = "Woman", Age = });
stuList.Add(new Student() { Name = "tiana2", Sex = "Woman", Age = });
stuList.Add(new Student() { Name = "tiana3", Sex = "Man", Age = });
stuList.Add(new Student() { Name = "tiana4", Sex = "Woman", Age = });
stuList.Sort();
foreach (Student stu in stuList)
{
Console.WriteLine("Name=" + stu.Name + ";Sex=" + stu.Sex + ";Age=" + stu.Age);
}
}
IComparer接口示例代码
定义学生类。
public class Student
{
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
}
自定义比较器AgeComparer,实现接口IComparer<Student>,对学生年龄进行比较。
public class AgeComparer : IComparer<Student>
{
public int Compare(Student x, Student y)
{
return x.Age.CompareTo(y.Age);
}
}
自定义比较器NameComparer,实现接口IComparer<Student>,对学生姓名进行比较。
public class NameComparer : IComparer<Student>
{
public int Compare(Student x, Student y)
{
return x.Name.CompareTo(y.Name);
}
}
调用List<T>.Sort方法实现stuList按照学生的年龄与姓名排序。
static void Main(string[] args)
{
List<Student> stuList = new List<Student>();
stuList.Add(new Student() { Name = "aki", Sex = "Man", Age = });
stuList.Add(new Student() { Name = "cki", Sex = "Woman", Age = });
stuList.Add(new Student() { Name = "dki", Sex = "Woman", Age = });
stuList.Add(new Student() { Name = "bki", Sex = "Man", Age = });
stuList.Add(new Student() { Name = "fki", Sex = "Woman", Age = });
stuList.Sort(new AgeComparer());
Console.WriteLine("按照年龄排序:");
foreach (Student stu in stuList)
{
Console.WriteLine("Name=" + stu.Name + ";Sex=" + stu.Sex + ";Age=" + stu.Age);
}
stuList.Sort(new NameComparer());
Console.WriteLine();
Console.WriteLine("按照名称排序:");
foreach (Student stu in stuList)
{
Console.WriteLine("Name=" + stu.Name + ";Sex=" + stu.Sex + ";Age=" + stu.Age);
}
}
原文链接:https://blog.csdn.net/yl2isoft/article/details/13613569
IComparable和IComparer接口的更多相关文章
- [0] 关于IComparable和IComparer接口和Comparer类
关于IComparable和IComparer接口 和 Comparer类 IComparable和ICompareframeworkr接口是.net 中比较对象的标准方式,这两个接口之间的区别如下: ...
- 对象的比较与排序:IComparable和IComparer接口
IComparable和ICompare 接口是.net framework 中比较对象的标准方式,这两个接口提供一个返回值类似(大于0 等于0 小于0)的比较方法,二者区别如下: . ICompar ...
- c# 实现IComparable、IComparer接口、Comparer类的详解
在默认情况下,对象的Equals(object o)方法(基类Object提供),是比较两个对象变量是否引用同一对象.我们要必须我自己的对象,必须自己定义对象比较方式.IComparable和ICom ...
- 数组自定义排序:IComparable和IComparer接口
首先先说一下IComparable和IComparer的区别,前者必须在实体类中实现,后者可以单独出现在一个排序类中,即此类只包含一个compare方法. Array类使用快速算法对数组中的元素进行排 ...
- C#的 IComparable 和 IComparer接口及ComparableTo方法的 区别(非常重要)
(1)https://blog.csdn.net/ios99999/article/details/77800819 C# IComparable 和 IComparer 区别 (2)https:// ...
- C#中的IComparable 和 IComparer 接口,实现列表中的对象比较和排序
借豆瓣某博主的话先对这两个接口进行一个解释: IComparable在要比较的对象的类中实现,可以比较该对象和另一个对象 IComparer在一个单独的类中实现,可以比较任意两个对象. 如果已经支持 ...
- 实现IComparable、IComparer接口
using System;using System.Collections.Generic; public class MyClass{ public class Employee:IComparab ...
- c# IComparable与IComparer接口
- C# IComparable接口、IComparer接口和CompareTo(Object x)方法、Compare()方法
在项目中经常会用到字符串比较,但是有时候对字符串的操作比较多,规则各异.比如有的地方我们需要用排序规则,有的地方需要忽略大小写,我们该如何写一个比较容易操作的比较方法呢?重新实现IComparer接口 ...
随机推荐
- oracle--CRS-0215 : Could not start resource 'ora.node2.ons'.
01,问题描述 安装10G+RAC集群,在node2进行vipca操作的时候发现这个问题 02,问题解决 原因是少了host的回环地址,当时删除错了 添加进去即可 127.0.0.1 localhos ...
- Linux进程启动/指令执行方式研究
1. 通过glibc api执行系统指令 0x1:system() glibc api system是linux系统提供的函数调用之一,glibc也提供了对应的封装api. system函数的原型为: ...
- .NET Standard和.NET Core是什么关系(转载)
.NET Standard vs .NET Core 问: I have read about the difference between .NET Standard and .NET Core, ...
- WPF WebBrowser抑制Suppress 弹出 脚本错误 对话框 但是样式改变 需要继续改善
1.添加引用 using System.Reflection;using System.Windows.Controls; 2.静态类扩展方法(this) public static class We ...
- WebService--导出excel并将excel发送到邮箱
1.利用NPOI生成EXCEL 2.导出excel之后发送邮件 //导出excel //path :保存路径 //dt:要导出的数据 public static bool DataTableToExc ...
- [世预赛] 中国vs关岛,关岛实力有限 国足或许可以赢其10个球,比分预测 10:0,8:0,13:0
[世预赛] 中国vs关岛 开赛时间:2019-10-10 20:00 继5比0大胜马尔代夫之后,国足迎来世预赛40强赛的第二场比赛,再次向世界杯发起冲击.10月10日,国足在广州迎战神秘之旅关岛. 1 ...
- Bootstrap3-导航条
1. 定义导航条 <!-- 导航条 navbar --> <div class="navbar nav-bar-default"> <ul class ...
- IOS—— strong weak retain assign 学习
转自:http://wenzongliang.iteye.com/blog/1746604 简单讲strong等同retain weak比assign多了一个功能,当对象消失后自动把指针变成nil,好 ...
- idea 忽略提交文件
https://blog.csdn.net/wangjun5159/article/details/74932433 https://blog.csdn.net/m0_38001814/article ...
- 分布式文件系统HDFS练习
本次作业要求:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3310 start-all.sh确保开启服务 在HDFS中为hadoop ...