Introduction
After I wrote part 1 about this subject, I posted an interview question on my blog on how to sort strongly typed collection and an alert reader Ben Hyrman posted a detailed answer to the interview question. Actually I think the answer presented by Ben is better than the solution I used in my first article. However the idea is the same - the difference is in the technique. I consider part 1 as for beginners, and part 2 for intermediate-advanced developers, as here we will explore Reflections, and dig deep into Generics. This article is based on the idea described in the article Generic IComparer
IComparer Interface
The IComparer interface declares only one method Compare, which takes 2 parameters to compare between them. There is a generic IComparer<T> to build a strongly typed comparer. For more information about the Compare method please consult the MSDN.
An overload of List<T>.Sort method accepts an IComparer<T> instance. The idea of our implementation is to set the sorting expression and pass it to the IComparer instance, and implement the Compare method so that it can compare according to the sort expression (sorting property and ascending or descending).
All your types that need to support sorting, need to implement IComparer<T>.
Background on old implementation
The first implementation of what we are going to explore now has a small issue. It is build with .Net 1.1 which has no support for generics. Although its mentioning that you need to create only one comparer to manage all possibilities, but that is not always the case. This will only work if all of your types have one base class that implements the IComparer interface.
Solving the issue in .Net 2.0
The idea is perfect, and applicable, but here we will enhance to use features of .Net 2.0. We will build a generic class named GenericComparer<T> that implements IComparer<T>. We will make typed Comparer classes that inherits from our new GenericComparer class. This is the major enhancement on the old implementation.
Overview on the solution
Before we dig deep, I'll describe the solution classes and interfaces.
Because we will still gain use of IComparable interface we will enhance this interface and add another method CompareTo that accepts 2 parameters, the first one we all know about is the object we want to compare with. The second parameter will be a string that should be the name of the property on which the comparison should be done e.g: FirstName property. This method will use .Net Reflection.
In our strongly typed collection, we will overload the sort method to accept the sorting property and sorting order. The Sort method will create an instance of a type that inherits from GenericComparer class with sorting expression and pass it to an overloaded Sort method that accepts an IComparer.
Creating a type (class) that implements our IComparable extension, as well as creating a typed Comparer class that inherits from GenericComparer and implements IComparer. On this class we will impement the Compare method so that it calls its base Compare method, this way we will not duplicate the code.
The Implementation
IComparableEx<T> Interface:
Product2 Class: Properties code is omitted for clarification.
Product2Collection Class:
Summary
Applying sorting to specific types can take several faces, what was mentioned here is not supposed to be the best or the most optimal solution, it just open the doors for representing and sharing techniques.
Download
Buidling Sortable Strongly Typed Collections (Visual Studio Project)
About Muhammad Mosa
 |
Sorry, no bio is available
This author has published 8 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
Avoid Using a Database as an API Integration Point
read more
Web Parts in SUB V2
read more
Eureka!
read more
C# and databases...
read more
Keep Writing SharePoint Web Parts Until (at least) 2006
read more
|
|
Please login to rate or to leave a comment.