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接口的更多相关文章

  1. [0] 关于IComparable和IComparer接口和Comparer类

    关于IComparable和IComparer接口 和 Comparer类 IComparable和ICompareframeworkr接口是.net 中比较对象的标准方式,这两个接口之间的区别如下: ...

  2. 对象的比较与排序:IComparable和IComparer接口

    IComparable和ICompare 接口是.net framework 中比较对象的标准方式,这两个接口提供一个返回值类似(大于0 等于0 小于0)的比较方法,二者区别如下: . ICompar ...

  3. c# 实现IComparable、IComparer接口、Comparer类的详解

    在默认情况下,对象的Equals(object o)方法(基类Object提供),是比较两个对象变量是否引用同一对象.我们要必须我自己的对象,必须自己定义对象比较方式.IComparable和ICom ...

  4. 数组自定义排序:IComparable和IComparer接口

    首先先说一下IComparable和IComparer的区别,前者必须在实体类中实现,后者可以单独出现在一个排序类中,即此类只包含一个compare方法. Array类使用快速算法对数组中的元素进行排 ...

  5. C#的 IComparable 和 IComparer接口及ComparableTo方法的 区别(非常重要)

    (1)https://blog.csdn.net/ios99999/article/details/77800819 C# IComparable 和 IComparer 区别 (2)https:// ...

  6. C#中的IComparable 和 IComparer 接口,实现列表中的对象比较和排序

    借豆瓣某博主的话先对这两个接口进行一个解释: IComparable在要比较的对象的类中实现,可以比较该对象和另一个对象 IComparer在一个单独的类中实现,可以比较任意两个对象. 如果已经支持 ...

  7. 实现IComparable、IComparer接口

    using System;using System.Collections.Generic; public class MyClass{ public class Employee:IComparab ...

  8. c# IComparable与IComparer接口

  9. C# IComparable接口、IComparer接口和CompareTo(Object x)方法、Compare()方法

    在项目中经常会用到字符串比较,但是有时候对字符串的操作比较多,规则各异.比如有的地方我们需要用排序规则,有的地方需要忽略大小写,我们该如何写一个比较容易操作的比较方法呢?重新实现IComparer接口 ...

随机推荐

  1. TJOI 2015 概率论(生成函数)

    题意 ​ 求一棵随机生成的有根二叉树(节点无标号,各种不同构的情况随机出现)叶子结点个数的期望. 思路 ​ 用生成函数做是个好题. ​ 我们考虑设 \(n\) 个节点,所有不同构二叉树叶子结点的总和为 ...

  2. EF Code Frist 执行 nuget命令

    1.Enable-Migrations -EnableAutomaticMigrations2.Add-Migration InitialCreate3.Update-Database -Verbos ...

  3. Visual Studio 调试系列5 检查变量(使用自动窗口和局部变量窗口)

    系列目录     [已更新最新开发文章,点击查看详细] 在调试时,“自动变量”和“局部变量”窗口会显示变量值. 仅在调试会话期间,这两个窗口才可用. “自动变量”窗口显示当前断点周围使用的变量. “局 ...

  4. 第五次实验报告:使用Packet Tracer理解OSPF路由协议

    目录 1 实验目的 2 实验内容 3. 实验报告 3.1 建立网络拓扑结构 4. 配置 4.1 配置并激活串行地址和以太网地址 4.1.1 R1 4.1.2 R2 4.1.3 R3 4.1.4 PC ...

  5. vue组件component没效果

    如果实在不知道问题所在,你就看看你的component的命名是不是驼峰命名

  6. 第22课 weak_ptr弱引用智能指针

    一. weak_ptr的概况 (一)weak_ptr的创建 1. 直接初始化:weak_ptr<T> wp(sp); //其中sp为shared_ptr类型 2. 赋值: wp1 = sp ...

  7. 微信JSSDK 扫描二维码

    <?php require_once('wxjssdk.class.php'); $weixin = new class_weixin(); $signPackage = $weixin-> ...

  8. 关于stm32f10x_conf.h文件

    简介 stm32f10x_conf.h文件有2个作用:①提供对assert_param运行时参数检查宏函数的定义.②将开发者用到的标准外设头文件集中在这个文件里面,而stm32f10x_conf.h又 ...

  9. 使用jedis操作redis常用方法

    在redis入门及在商城案例中的使用中简单介绍了下使用jedis如何操作redis,但是其实方法是跟redis的操作大部分是相对应的.我这里做下记录 1.String类型操作 public class ...

  10. 【CTS2019】珍珠(生成函数)

    [CTS2019]珍珠(生成函数) 题面 LOJ 洛谷 题解 lun题可海星. 首先一个大暴力\(sb\)的\(dp\)是设\(f[i][S]\)表示当前考虑完了前\(i\)个珍珠,\(S\)集合中这 ...