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, WA. James has worked on several key Microsoft products such as Internet Explorer and Bing. James can be reached at jamccaff@microsoft.com.