zoukankan      html  css  js  c++  java
  • 实现IComparable、IComparer接口

    using System;
    using System.Collections.Generic;

    public class MyClass
    {
    public class Employee:IComparable<Employee>
    {
    public int EmpID;
    public string YearsOfSvc = "1";
    public Employee(int id)
    {
    EmpID = id;
    }
    public Employee(int id , string ys)
    {
    this.EmpID = id;
    this.YearsOfSvc = ys;
    }
    public int CompareTo(Employee rhs)
    {
    return this.EmpID.CompareTo(rhs.EmpID);
    }
    public int CompareTo(Employee rhs,Employee.EmployeeComparer.ComparisonType which)
    {
    switch(which)
    {
    case Employee.EmployeeComparer.ComparisonType.EmpID:
    return this.EmpID.CompareTo(rhs.EmpID);
    break;
    case Employee.EmployeeComparer.ComparisonType.Yrs:
    return this.YearsOfSvc.CompareTo(rhs.YearsOfSvc);
    break;
    }
    return 0;
    }
    public static Employee.EmployeeComparer GetComparer()
    {
    return new Employee.EmployeeComparer();
    }
    public class EmployeeComparer:IComparer<Employee>
    {

    private ComparisonType whichComparison;
    public enum ComparisonType
    {
    EmpID,
    Yrs
    }
    public int Compare(Employee lhs,Employee rhs )
    {
    return lhs.CompareTo(rhs,whichComparison);
    }
    public Employee.EmployeeComparer.ComparisonType WhichComparison
    {
    get{return whichComparison;}
    set{whichComparison = value;}
    }
    }
    }

    public static void RunSnippet()
    {
    List<Employee> list = new List<Employee>();
    list.Add(new Employee(1,"how"));
    list.Add(new Employee(0,"are"));
    list.Add(new Employee(-1,"you"));
    list.Add(new Employee(4,"fine"));
    list.Add(new Employee(3,"thanks"));
    Employee.EmployeeComparer c = Employee.GetComparer();
    c.WhichComparison = Employee.EmployeeComparer.ComparisonType.Yrs;

    list.Sort(c);
    for(int i = 0; i < list.Count; i++)
    {
    Console.WriteLine(list[i].YearsOfSvc);
    }

    }

    #region Helper methods

    public static void Main()
    {
    try
    {
    RunSnippet();
    }
    catch (Exception e)
    {
    string error = string.Format("--- The following error occurred while executing the snippet: {0} ---", e.ToString());
    Console.WriteLine(error);
    }
    finally
    {
    Console.Write("Press any key to continue...");
    Console.ReadKey();
    }
    }

    private static void WL(object text, params object[] args)
    {
    Console.WriteLine(text.ToString(), args);
    }

    private static void RL()
    {
    Console.ReadLine();
    }

    private static void Break()
    {
    System.Diagnostics.Debugger.Break();
    }

    #endregion
    }

  • 相关阅读:
    linux /usr/bin/ld: cannot find -lxxx
    chm手册显示已取消到该网页的导航
    安装 composer SSL operation failed with code 1
    protoc-gen-php --php_out: protoc-gen-php: Plugin output is unparseable.
    Lamp 安装(CentOS6.6, php-5.4.39, httpd-2.4.12, mysql-5.6.24)
    SUSE Linux Enterprise Server 11 软件源
    vmware 下linux 共享文件夹消失
    thrift 安装历程
    隐藏apache服务器信息
    固定总金额、还款天数、还款次数,计算每日每次的还款金额
  • 原文地址:https://www.cnblogs.com/jinweijie0527/p/4952960.html
Copyright © 2011-2022 走看看