Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Sorting in the .NET Framework  : Page 4

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.


advertisement
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. Consider the class Recipe shown in Listing 3.

Listing 3 shows that when you implement IComparable for the Recipe class, you are required to implement the CompareTo() method. In this definition, you simply return the result of comparing the private variable that holds the value for RecipeID to the RecipeID property of the object being passed. Although this is a simple example, if I had to, I could have made the comparison more complex by involving more than one property of the class.



Figure 2. Multidimensional Sorting Diagram: This diagram gives a visual representation of the different ways that a multidimensional array can be sorted.

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.

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.

Sort Type

Description

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.


Table 2. Sorting Types For Multidimensional Arrays: Sorting multidimensional arrays can produce different results based on the manner in which it was sorted.

The source code for this article includes a class library to facilitate multidimensional sorting. A demo application is also included to interactively show these sorting methods in action.

Don't Reinvent the Wheel!
Sadly, many developers, especially those from a VB6 background, are slow to gain a fundamental understanding of the richness of the class library that the .NET library boasts. Hopefully, this article can help save developers from reinventing the wheel by coding their own sorting routines.



Joe Agster is a .NET software developer based in Las Vegas. He works as a software consultant and developer specializing in the .NET Framework. He can be found on the Web at www.geekyfrog.com.
Comment and Contribute

 

 

 

 

 


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

 

 

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