调用LIST的Sort的时候会调用IComparer的默认实现,quicksort会调用每个元素的CompareTo的IComparable实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ComparerTest
{
class Employee : IComparable<Employee>
{
private int empID;
private int yearOfSvc = 1; public Employee(int empID)
{
this.empID = empID;
} public Employee(int empID, int yearOfSvc)
{
this.empID = empID;
this.yearOfSvc = yearOfSvc;
} public override string ToString()
{
return "ID: " + empID.ToString()
+ " yearOfSvc: " + yearOfSvc.ToString();
} public bool Equals(Employee other)
{
if (this.empID == other.empID)
{
return true;
}
else
{
return false;
}
} //获取Comparer对象的静态方法
public static EmployeeComparer GetComparer()
{
return new Employee.EmployeeComparer();
}
//比较委托回Employee
//Employee使用整形的默认
//CompareTo方法
public int CompareTo(Employee rhs)
{
return this.empID.CompareTo(rhs.empID);
} //自定义Comparer要调用的特殊表现
public int CompareTo(Employee rhs,
Employee.EmployeeComparer.ComparisonType whichComparision)
{
switch (whichComparision)
{
case Employee.EmployeeComparer.ComparisonType.EmpID:
return this.empID.CompareTo(rhs.empID);
case Employee.EmployeeComparer.ComparisonType.Yrs:
return this.yearOfSvc.CompareTo(rhs.yearOfSvc);
}
return 0;
} //实现IComparer的内嵌类
public class EmployeeComparer : IComparer<Employee>
{
private Employee.EmployeeComparer.ComparisonType whichComparision; public enum ComparisonType
{
EmpID,
Yrs
}; public bool Equals(Employee lhs, Employee rhs)
{
return this.Compare(lhs, rhs) == 0;
}
//让Empolyee对象自己比较
public int Compare(Employee lhs, Employee rhs)
{
return lhs.CompareTo(rhs, WhichComparison);
} public int GetHashCode(Employee e)
{
return e.GetHashCode();
} public Employee.EmployeeComparer.ComparisonType WhichComparison
{
set
{
whichComparision = value;
}
get
{
return whichComparision;
} }
}
}
class Program
{
static void Main(string[] args)
{
List<Employee> empArray = new List<Employee>(); Random ra = new Random(); for (int i = 0; i < 5; i++)
{
empArray.Add(new Employee(ra.Next(10)+100,ra.Next(20)));
} for (int i = 0; i < empArray.Count; i++)
{
Console.Write("\n{0}",empArray[i].ToString());
}
Console.WriteLine();
Console.WriteLine();
Employee.EmployeeComparer c = Employee.GetComparer(); c.WhichComparison = Employee.EmployeeComparer.ComparisonType.EmpID; empArray.Sort(c); for (int i = 0; i < empArray.Count; i++)
{
Console.Write("\n{0}", empArray[i].ToString());
}
Console.WriteLine();
Console.WriteLine();
Employee.EmployeeComparer c2 = Employee.GetComparer(); c.WhichComparison = Employee.EmployeeComparer.ComparisonType.Yrs; empArray.Sort(c2); for (int i = 0; i < empArray.Count; i++)
{
Console.Write("\n{0}", empArray[i].ToString());
}
Console.ReadLine();
}
}
}

C#_IComparer实例 - 实现ID或者yearOfscv排序的更多相关文章

  1. php实例根据ID删除mysql表中的数据

    在动态网站开发中,我们经常要根据ID删除表中的数据,例如用户删除帖子,就需要根据ID删除帖子.本文章向大家介绍php根据ID删除表中数据的实例,需要的朋友可以参考一下本文章的实例. php实例根据ID ...

  2. liferay中如何获取实例的id和portletId

    在Portlet中request分为两种renderRequet和actionRequest而portlet需要取得实例Id的时候都在renderRequest的时候才可以取到,如下例子 Portle ...

  3. C#_IComparable实例 - 对象ID进行排序

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Comp ...

  4. java学习--自定义类的实例的大小比较和排序

    我们知道Object类有一个equals方法,用于比较两个对象是否相等 我们只要在自定义类中重写了equals方法(若不重写就是比较两个实例的地址,相当于==)就可以用来比较该类的两个实例是否相等 问 ...

  5. 如何将RAC数据库的 RMAN Disk 备份 Restore 到另一个节点上的单个实例 (Doc ID 415579.1)

    HowTo Restore RMAN Disk backups of RAC Database to Single Instance On Another Node (Doc ID 415579.1) ...

  6. 让mysql返回的结果按照传入的id的顺序排序

    比如id为 1,3,5,44,66,32,21,6 那么返回的结果顺序也是这个顺序   $sql = "select * from ".$this->tableName(). ...

  7. mysql 对数据的自增ID重新进行排序

    创建表格时添加: create table table1(id int auto_increment primary key,...) 创建表格后添加: 删除原有主键: ALTER TABLE `ta ...

  8. [C][代码实例]整型数组二分排序

    #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h& ...

  9. 【Python】【demo实验9】【练习实例】【三数排序】

    原题: 输入三个整数x,y,z,请把这三个数由小到大输出. 我的解法: #!/usr/bin/python # encoding=utf-8 # -*- coding: UTF-8 -*- # 输入三 ...

随机推荐

  1. 【转】禁止seekbar的拖动事件

    原文网址:http://blog.csdn.net/ansionnal/article/details/8229801 当然是可以的! 其实是 onTouchEvent 事件时,不让他传递事件就行了! ...

  2. WCF学习笔记(二):简单调用

    转:http://www.cnblogs.com/wengyuli/archive/2009/11/08/1598428.html 一个通信会话过程有两个部分组成,客户端和服务端,他们要进行会话就必然 ...

  3. POJ 3377 Ferry Lanes

    虽然它出现在dp专场里···但是我第一反应是一道最短路题···不过幸好它出现在dp专场里···因为我不怎么会dijstra什么的··· 题意:一条河上有N+1对码头,每个相邻码头之间需要一定时间到达, ...

  4. The new Portable Class Library for SQLite z

    Microsoft Open Technologies has recently released a Portable Class Library for SQLite. Thanks to it, ...

  5. opencv开发的程序分发给客户时所需要的dll文件

    这里主要讲在其他裸机,没有搭建开发环境机器上运行自己开发的程序. 为了测试,我专门用visualbox搭建了一个虚拟机(主机和虚拟机都是win7系统) 在发给别人程序运行出现错误:msvcp100d. ...

  6. js实现收缩菜单效果

    废话不多说,直接上代码: 有注释 <head> <title></title> <style type="text/css"> di ...

  7. SQL Server: Difference Between Locking, Blocking and Dead Locking

    Like ever, today’s article of Pinal Dave was interesting and informative. After, our mutual discussi ...

  8. Android Binder------ServiceManager启动分析

    ServiceManager启动分析   简述: ServiceManager是一个全局的manager.调用了Jni函数,实现addServicew getService checkService ...

  9. MSSQLSERVER- CharIndex的妙用,找出有妙用

    CharIndex 1:CharIndex语法: CharIndex(expression1,expression2[,start_location]) 2:参数 expression1 一个表达式, ...

  10. DHCP Option 60 的理解

    原文地址: http://blog.163.com/chenqioulin_1983/blog/static/83216232010109104430251/   首先还是看看RFC咋说的吧.DHCP ...