Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Sorting in the .NET Framework  : Page 3

You don't need to code your own sorting routine—the .NET Framework can do it for you! Read this article to get a basic, yet comprehensive, understanding of sorting and comparing objects in the .NET Framework.

Customized Sorting with IComparer
Another way to sort objects is to use a comparer class. A comparer class is one that implements the IComparer interface. This is not to be confused with IComparable, which has a different use.

When you use a comparer class to sort objects, you are overriding the default comparing method defined by the object's CompareTo() method. As a matter of fact, if you use a comparer class to sort a list of objects, the class definition for the object doesn't even need to implement IComparable. Thus, the Sort() methods defined in Array and ArrayList requires that all objects being sorted either implement the IComparable interface, or a class implementing IComparer be passed. The .NET Framework defines several comparer classes for you to use. One of them, the CaseInsensitiveComparer class, allows you to sort strings by ignoring their casing. So in this case, the .NET framework ignores the String class' CompareTo method, and instead uses the rules defined in the CaseInsensitiveComparer class. The following code illustrates this feature:

Dim aryLastNames() As String = {"sMiTh, ZULU", "smith, john&", "SMITH, TerrY"} Array.Sort(aryLastNames, New CaseInsensitiveComparer) ' aryLastNames order: ' ' smith, john ' SMITH, TerrY ' sMiTh, ZULU

In the opening paragraphs I talked about sorting objects like System.IO.FileInfo. IComparable, so using a comparer class is absolutely necessary. In any case, defining your own comparer class for FileInfo objects allows for maxmium flexibility when determining how they should be sorted. For example, a program that displays files on disk to the user and allows the user to sort these files by name, extension, length, and whatever else (just like Windows Explorer) would take advantage of a comparer class to implement this functionality. The download for this article provides a demonstration of using IComparer sort an array of FileInfo objects.

IComparable vs. IComparer
Because of the way they're named, these two interfaces are oftentimes confused. But after exploring their uses, the differences should be clear. The following table shows a side-by-side comparison between the two.



Comparing method must be defined within the class for which it is used.

Comparing method is defined in a separate class.

Can provide only one method for which to compare objects of the class.

Many classes can be defined, each classthat can contain a distinct comparing method.

Because it is defined within the class, it can access private variables on which to sort.

Can only sort on public properties and fields.

If both interfaces are defined for sorting, this one is not used.

If both interfaces are defined for sorting, this one is used.

Provides a default comparing method for the class in which it is defined.

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.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date