Multilevel Sorting with IComparable and IComparer: Listing 3.
The CompareTo method.
public int CompareTo(City other)
{
if (String.Compare(this.name, other.name) == 0 &&
this.population == other.population &&
RegionCompare(this.region, other.region) == 0)
return 0;
else if (this.population > other.population)
return 1;
else if (this.population == other.population &&
RegionCompare(this.region, other.region) > 0)
return 1;
else if (this.population == other.population &&
RegionCompare(this.region, other.region) == 0 &&
String.Compare(this.name, other.name) > 0)
return 1;
else
return -1;
}
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].