Published: 20 Oct 2006
By: Muhammad Mosa

We all know about Strongly Typed Collections and its functionality, also we have heard about .Net Generics that is used to build strongly typed collections. Generally in collections you can add, delete, search & sort objects in it. The issue we will discuss in this article is Sorting Strongly Typed Collections.

Introduction

In MSDN, the definition of the Array.Sort method is: "Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array". which means that any object in the array that you wish to sort must implement the IComparable interface. I think most of .Net Framework implements the IComparable interface like Int32, Int16, String, Decimal etc. but what about our own types? The Classes and structures we build ourselves? Do we still be able to sort them? Yes we can do that, but note that those will be complex type, which means that you have to know what is the parameter you are going to use to sort your own defined types, and this what we are going to see in the next sections.
If you tried to sort and array or a strongly typed collection that is containing your defined type, and this type do not implement the IComparable interface, you will get an exception which is InvalidOperationException.

Building Strongly Typed Collection (The Simple Way)

To build a strrongly typed collection, there are many ways, like implementing ICollection Interface with some other interfaces, or making the abstract class CollectionBase your base class and start coding its abstract methods etc. .
Well the Generic feature that .Net 2.0 gives us today is wonderful, as you can build a strongly typed collection that has most of the functionality you wish to have in a collection in 10 seconds. For example, suppose you have a class named Product, to build its strongly typed collection, you just have to make the generic List<T> your base class as the following.

Easy isn't it.

The Generic IComparable Interface

The normal IComprabale interface has only one method CompareTo which takes one object parameter. In Generic the same interface exists IComprabale<T> -T is place holder for your typed you can call it template- but it lets you replace the native object parameter with your class type parameter. You call it strongly typed interface.

Implementing IComparable<T> Interface

Now you have to implement the IComprable<T> interface to be able to support sort feature in your strongly typed collection. Your class should implement it, which means the Product class. First you have to know more about the CompareTo method. This method returns an integer that should have the following meanings:

  • Less than zero if this instance is less than the supplied parameter.
  • Zero if this instance is equal to the supplied parameter.
  • Greater than zero if this instance is greater than the supplied parameter.

See the following for an example:

Sorting in action

The following code demonstrates the sortable strongly typed collection:

On good question may appear, what if I don't want sort by ProductName and I want to sort by any other fields? Well you can but this will cost extra coding. My suggestion is to make an enumeration that contains all your sorting options. Then have a static member in your class, in our case the Product class; name it SortBy or whatever you like and make it of the type enumeration. Then before the call to the Sort method set the SortBy property to the desired value. Now you will also need to update your CompareTo method to check for your sort option. The following code shows the updated class with the sort options.

Below is the main consol program updated:

Summary

It is important to know such things in .Net, as they are commonly used if you are going to use strongly typed collections. In addition, if you tried the BinarySearch method you should get results as it is using the same mechanism. Attached project contains a console application that contains the code described in this article.

Downloads

SortableCollectionDemo

Continue reading and see our next or previous articles Next Article >>

About Muhammad Mosa

Sorry, no bio is available

This author has published 8 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


Building Sortable Strongly Typed Collections Part 2
We all know about strongly typed collections and its functionality, also we have heard about .net Ge...
Top
 
 
 

Discussion


Subject Author Date
placeholder Nice, but may be cleaner with Comparison Simone Busoli 5/4/2007 8:27 PM
Sorry for the formatting Simone Busoli 5/4/2007 8:33 PM

Please login to rate or to leave a comment.

Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.