Multilevel Sorting with IComparable and IComparer: Listing 1.

A single default sort order.

class City : IComparable<City>
  private string name; // e.g. "boston"
  private double population; // in millions
  private string region; // "north", "south", "east", "west"
  public City(string name, double population, string region) { . . }
  public override string ToString() { . . }
  public int CompareTo(City other) { . . }
  static int RegionCompare(string regionA, string regionB) { . . }
  static int RegionValue(string region) { . . }

About the Author

Dr. James McCaffrey works for Microsoft Research in Redmond, Wash. He has worked on several Microsoft products including Azure and Bing. James can be reached at

comments powered by Disqus


.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events