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 [email protected].

comments powered by Disqus

Featured

  • 'Dev Home' Update Leads Developer Goodies in AI-Powered Windows 11 Update

    Along with today's new AI-powered Windows 11 update come new goodies for developers, including a new edition of Dev Home, a preview offering described as a "control center" providing coding-focused features and functionality.

  • Community Dev Gives VS Code Python Some YAPF

    The latest update to Python in Visual Studio Code includes a new extension for Python formatting that was contributed by a member of the open source community.

  • Devs Demand Visual Studio 2022 Ditch Old .NET Framework Dependencies

    Developers commenting on a Microsoft post about performance improvements in the upcoming .NET 8 demanded the company end Visual Studio 2022's dependency on the old .NET Framework.

  • Microsoft Remakes Azure Quantum Dev Kit with Rust, 'and It Runs in the Browser!'

    "The' tl;dr' is that we rewrote it (mostly) in Rust which compiles to WebAssembly for VS Code or the web, and to native binaries for Python."

Subscribe on YouTube