zoukankan      html  css  js  c++  java
  • C# 重写IComparer 接口

    首先定义比较类 继承自IComparer<Racer>

     1 public class RacerComparer : IComparer<Racer>
     2     {
     3         public enum CompareType
     4         {
     5             FirstName, LastName, Country, Wins
     6         }
     7         private CompareType compareType;
     8         public RacerComparer(CompareType compareType)
     9         {
    10             this.compareType = compareType;
    11         }
    12         public int Compare(Racer x, Racer y)
    13         {
    14             if (x == null && y == null) return 0;
    15             if (x == null) return -1;
    16             if (y == null) return 1;
    17 
    18             int result;
    19             switch (compareType)
    20             {
    21                 case CompareType.FirstName:
    22                     return string.Compare(x.FirstName, y.FirstName);
    23                 case CompareType.LastName:
    24                     return string.Compare(x.LastName, y.LastName);
    25                 case CompareType.Country:
    26                     result = string.Compare(x.Country, y.Country);
    27                     if (result == 0)
    28                         return string.Compare(x.LastName, y.LastName);
    29                     else
    30                         return result;
    31                 case CompareType.Wins:
    32                     return x.Wins.CompareTo(y.Wins);
    33                 default:
    34                     throw new ArgumentException("Invalid Compare Type");
    35             }
    36         }
    37     }
    View Code
     1 class Racer : IComparable<Racer>
     2     {
     3         public int Id { get; private set; }
     4         public string FirstName { get; set; }
     5         public string LastName { get; set; }
     6         public string Country { get; set; }
     7         public int Wins { get; set; }
     8         public override string ToString()
     9         {
    10             return String.Format("{0} {1}", FirstName, LastName);
    11         }
    12         public Racer(int id, string firstName, string lastName, string country)
    13             : this(id, firstName, lastName, country, wins: 0)
    14         {
    15         }
    16         public Racer(int id, string firstName, string lastName, string country, int wins)
    17         {
    18             this.Id = id;
    19             this.FirstName = firstName;
    20             this.LastName = lastName;
    21             this.Country = country;
    22             this.Wins = wins;
    23         }
    24         public int CompareTo(Racer other)
    25         {
    26             if (other == null) return -1;
    27             int compare = string.Compare(this.LastName, other.LastName);
    28             if (compare == 0)
    29                 return string.Compare(this.FirstName, other.FirstName);
    30             return compare;
    31         }
    32     }
    View Code

     调用比较方法

    1  var graham = new Racer(7, "Graham", "Hill", "UK", 14);
    2             var emerson = new Racer(13, "Emerson", "Fittipaldi", "Brazil", 14);
    3             var mario = new Racer(16, "Mario", "Andretti", "USA", 12);
    4 
    5             var racers = new List<Racer>(20) { graham, emerson, mario };
    6  racers.Sort(new RacerComparer(RacerComparer.CompareType.Country));
    View Code
  • 相关阅读:
    表达式和计算的描述
    表达式和计算的描述
    递归算法浅谈
    编程基本功训练:流程图画法及练习
    【2012.1.24更新】不要再在网上搜索eclipse的汉化包了!
    VS2008下直接安装使用Boost库1.46.1版本号
    android关键组件service服务(一)
    U盘安装咱中国人自己的操作系统UbuntuKylin14.04LST(超具体原创图文教程)
    数据流图的画法
    匈牙利算法
  • 原文地址:https://www.cnblogs.com/farmer-y/p/5970117.html
Copyright © 2011-2022 走看看