Multilevel Sorting with IComparable and IComparer: Listing 6.

A class with default and non-default sort orders.

public class City : IComparable<City>
{
  private string name;
  private double population;
  private string region;

  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) { . . }

  public class SortRoutines
  {
    public class SortByNameAscending : IComparer<City> { . . }
    public class SortByNameDescending : IComparer<City> { . . }
    public class SortByPopulationName : IComparer<City> { . . }
  }
}

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 jamccaff@microsoft.com.

comments powered by Disqus

Featured

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events