C# 重写IComparer 接口

Posted 指间的徘徊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于C# 重写IComparer 接口的主要内容,如果未能解决你的问题,请参考以下文章

c# IComparable与IComparer接口

C#中的IComparable和IComparer接口

C#中的IComparable和IComparer接口

C#接口汇总

C#接口汇总

在C#语言开发程序中,以下关于IComparable<T>和IComparer<T>的说法中错误的是()。(选择一项)