博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#_IComparer实例 - 实现ID或者yearOfscv排序
阅读量:6294 次
发布时间:2019-06-22

本文共 4064 字,大约阅读时间需要 13 分钟。

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

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ComparerTest{    class Employee : IComparable
{ 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
{ 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
empArray = new List
(); 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(); } }}

 

 

转载于:https://www.cnblogs.com/MarchThree/p/3720473.html

你可能感兴趣的文章
Android SharedPreferences
查看>>
css面试题
查看>>
Vue组建通信
查看>>
用CSS画一个带阴影的三角形
查看>>
前端Vue:函数式组件
查看>>
程鑫峰:1.26特朗.普力挺美元力挽狂澜,伦敦金行情分析
查看>>
safari下video标签无法播放视频的问题
查看>>
01 iOS中UISearchBar 如何更改背景颜色,如何去掉两条黑线
查看>>
对象的继承及对象相关内容探究
查看>>
Spring: IOC容器的实现
查看>>
Serverless五大优势,成本和规模不是最重要的,这点才是
查看>>
Nginx 极简入门教程!
查看>>
iOS BLE 开发小记[4] 如何实现 CoreBluetooth 后台运行模式
查看>>
Item 23 不要在代码中使用新的原生态类型(raw type)
查看>>
为网页添加留言功能
查看>>
JavaScript—数组(17)
查看>>
Android 密钥保护和 C/S 网络传输安全理论指南
查看>>
以太坊ERC20代币合约优化版
查看>>
Why I Began
查看>>
同一台电脑上Windows 7和Ubuntu 14.04的CPU温度和GPU温度对比
查看>>