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
原文地址:https://www.cnblogs.com/farmer-y/p/5970117.html