Multilevel Sorting with IComparable and IComparer: Listing 2.

Calling List.Sort using the default ordering.

List<City> list = new List<City>();

list.Add(new City("san diego", 1.2, "west"));
list.Add(new City("phoenix", 1.7, "west"));
list.Add(new City("chicago", 1.2, "north"));
list.Add(new City("boston", 1.0, "east"));
list.Add(new City("nashville", 1.2, "south"));
list.Add(new City("houston", 1.7, "west"));

foreach (City c in list) {
  Console.WriteLine(c.ToString());
}

list.Sort(); 

foreach (City c in list) {
  Console.WriteLine(c.ToString());
}

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.