Multilevel Sorting with IComparable and IComparer: Listing 4.
Several sort orders but no default order.
public class City
{
private string name;
private double population;
private string region;
public City(string name, double population, string region) { . . }
public override string ToString() { . . }
public class SortByNameAscending : IComparer<City> { . . }
public class SortByNameDescending : IComparer<City> { . . }
public class SortByPopulationName : IComparer<City> { . . }
}
About the Author
Dr. James McCaffrey directs the data science and research efforts at Quaetrix, a data analytics company located near Redmond, Washington. Before joining Quaetrix, James was a senior research engineer at Microsoft. James can be reached at [email protected].