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
  • 相关阅读:
    ABOUT JAD 反编译工具
    Linux下/proc目录简介(文章来源于http://blog.csdn.net/zdwzzu2006/article/details/7747977)
    用Axis2实现Web Service(简单的axis实现)
    初探STRUTS
    JAVA 导出Excel2003格式文件实现代码
    JAVA Httpclient3.x与Httpclient4.x代码对比(post方法)
    MyEclipse使用心得(快捷方式)
    Iptables详解(文章来源于http://blog.chinaunix.net/uid-22780578-id-3346350.html)
    C函数篇(Timer函数)
    C函数篇(wait函数)
  • 原文地址:https://www.cnblogs.com/farmer-y/p/5970117.html
Copyright © 2011-2022 走看看