Introduction
When I'm testing new applications, techniques, etc., I like having a standard set of data, usually a generic List of objects, that I can test with. Recently, I was testing a report generating library, and I found myself spending an annoying amount of time manipulating the data objects so that they would process in the correct order to test my report writer.
I added an IComparable
interface to my class so that I could more easily change the printing order of the objects. This was okay, but if possible, I would like to avoid cluttering my classes with test code.
Since my policy as a programmer is that if I've had to do it more than a handful of times I should think about how to create a utility to do it, I decided to create a generic way to sort object arrays on the fly.
So I created a class which is basically a wrapper around another class for sorting purposes. There is a static method which accepts as parameters the array of objects and a delegate pointing to the sorting method. It creates an array of the sorting class, injecting the original objects into the sorting objects, runs the sort method, and recreates the array in the new order and returns it.
Background
The IComparable
interface is used to create a sort order for an array of objects. If you add it to a class and implement it, you will get a single method, CompareTo
. This method receives a single parameter, which is the object that you need to compare to the current object. An integer value is returned indicating whether the current object needs to be before or after the passed-in object in the array.
Using the Code
Once you download the cSorting
class and make it available to your project, all you need is a sorting method. This method should take two parameters, which are the two objects to be sorted. It should return an integer value, which is:
- < 0 if the first object comes before the second
- > 0 if the second object comes before the second
- = 0 if the objects are the same
Here's an example of a sorting method. We are assuming that we are working with an array of cPerson
objects. The class cPerson
has two string properties: name
and birthState
. We want the cPerson
s sorted by state of birth, and within each state, by name.
public static int SortingByStateAndName(object x, object y) {
cPerson p1 = (cPerson)x;
cPerson p2 = (cPerson)y;
int compare = String.Compare(p1.birthState, p2.birthState);
if (compare == 0) {
compare = String.Compare(p1.name, p2.name);
}
return compare;
}
Now, all you need to do is call the static sorting method:
cPerson[] persons = CreateYourPersonArray();
cPerson[] newPersons = cSorting.SortingThisArray(persons, SortingByStateAndName);
Points of Interest
Even if you don't need to sort objects, it might be worth a peek into the cSorting
class just to see how to use IComparable
.
History
No changes yet.
I've been a developer/analyst for over twenty years, hopping from language to language. C, in all its incarnations (K/R, Borland, C++, C#) is my preference.