- Comparing method must be defined within the class for which it is used.
- Can provide only one method for which to compare objects of the class.
- Because it is defined within the class, it can access private variables on which to sort.
- If both interfaces are defined for sorting, this one is not used.
- Provides a default comparing method for the class in which it is defined.
- Comparing method is defined in a separate class.
- Many classes can be defined, each classthat can contain a distinct comparing method.
- Can only sort on public properties and fields.
- If both interfaces are defined for sorting, this one is used.
- Provides an alternative sorting method for the class for which it is used.
Table 1. IComparable vs. IComparer: This table breaks down the differences between these two interfaces.
Basically, IComparable is used to define a default comparing method, and IComparer is used to override or use in the absence of the default comparing method.
Comparing Objects with Operator Overloading
Operator overloading is the practice of defining a use for an operator, such as the plus sign + or equality sign =, for the purpose of comparing objects. Although only loosely related to sorting, operator overloading is a nice, powerful feature traditionally available in languages such as C++. Currently in .NET, operator overloading is a feature only supported in C#. However, in .NET 2.0 (code-named Whidbey), operator overloading will be introduced in VB.NET.
A good example of an object that uses overloaded operators is the String class. When you compare two strings using the equal sign = (== in C#), you are implicitly using an overloaded operator. Considering the Recipe class explained before, you can easily extend what has already been defined to includes operators for equality, less than, and greater than operations. Listing 2 shows the Recipe class modified from above to include these operator definitions.
Making Objects Comparable Using IComparable
Implicitly, all primitive types implement an interface called IComparable. This interface defines one method, CompareTo(), that allows a comparison between itself and another instance of the same type. This means that all basic data types, such as integers, strings, and even Booleans have a CompareTo method on it.
In most cases, your class will be sorted based upon one of its properties, which will most likely be of a primitive type. This makes implementing the IComparable interface even easier.
Sorting Multi-dimensional Arrays
The developer community is abuzz with discussion of sorting multidimensional arrays. Strangely enough, the .NET Framework doesn't support the sorting of multidimensional arrays. As a matter of fact, if you try to pass a multidimensional array into Array.Sort method you will get an exception. This leaves many developers confused on how to deal with sorting arrays of more than one dimension. Fortunately, there is a workaround.
The way to approach this problem is to turn a multidimensional array into a jagged array. A jagged array is basically an array of arrays. In other words, the array's element type is also of type array. The key advantage of jagged arrays over multidimensional arrays is that jagged arrays can dynamically scale to infinite dimensions, whereas multidimensional arrays are fixed in their rank, or number of dimensions. And as previously stated, jagged arrays can be sorted.
Different methods of sorting a multidimensional array produce different resuts. The first method is called full sort. This method sorts all of the dimensions. The second method performs a surface sort, or sort on only the first dimension, using the second and subsequent dimensions as tie breakers. The third method sorts the entire array as if it were a one-dimensional array. This method is called flat sorting.
When sorting the array in either a structured or flattened manner, the final thing to consider is its dimensional direction. The previous discussion assumed top-down sorting—as if you were starting from the first dimension and working your way down to the last one. However, it's feasible to sort from the bottom-up, or working from the last dimension to the first. Figure 2 illustrates the six possible ways that a multidimensional array can be sorted. These ways are described in Table 2.
Full Sort : From the starting dimension to the end, each dimension is sorted and the restructuring of the elements of that dimension affects the sorting of the subsequent dimensions.
Surface Sort : From the starting dimension to the end, each element is compares, and in the event of a tie, the next element in the dimension serves as the tie-breaker.
Flat Sort : The array is treated as a one-dimensional array for sorting.