Generic Comparer

well long time no c k here i m

some days ago i was working on my project , i felt a need to write a generic comparer, so i came up with this implementation, but the thing is that why i need this , have a look at my problem ,

i have some objects of different types, each type has a different set of properties, now i want to perform sorting and searching operation on the list of objects, the sorting and searching ke might be different at differetn times.

like we have a list of objects of type c1 widh p1 and p2 properties, i have 2 combos on my page each contains same list of objs, but sorted differently , one sorted on p1 and 2 sorted on p2, so what might be solution,

one possible solution might be that we impelement Icomparer for each properties of each type that we want to be sorted/searched ( I assume that we are going to use array.sort, array.Binarysearch wich requires IComparer object to perform the specified operation) , but that wuld be inefficient , infact, when new properties added to the type then we have to add new Icomparer implementations 😦 to much job for us 😛

so i came up wid this generic comparision ,

the idea is that we implement a single ICompareble impelemenation, and parametrizd it with the type of object that we are going to compare and the Property name on which the comparision is being performed . hold a default compare object, in compare method get the value of the property and compare it using default comparer, the default comparer will chk the type of value and perform comparision accordingly 🙂

Generic Comparer

here is the code


public class GenericComparer :IComparer


private PropertyInfo propertyInfo;

private Comparer comparer;

public PropertyInfo PropertyInfo




return this.propertyInfo;



private GenericComparer()



public GenericComparer(Type objectType, string comparisionProperty)




propertyInfo = objectType.GetProperty(comparisionProperty);

this.comparer = new Comparer(System.Globalization.CultureInfo.CurrentCulture);


catch (Exception ex)


throw new Exception(“Property ‘” + comparisionProperty + “‘ not found “+ex.Message);



public int Compare(object x, object y)


object valx= this.propertyInfo.GetValue(x,null);

object valy = this.propertyInfo.GetValue(y, null);

return this.comparer.Compare(valx, valy);





using this will be easier , a simple example ,


Class1 c = new Class1();

Class1 d = new Class1();

c.J = 5;

d.J = 2;

GenericComparer intComparer = new GenericComparer(typeof(Class1), “J”);


c.V = “C”;

d.V = “A”;

GenericComparer stringComparer = new GenericComparer(typeof(Class1), “V”);

Console.WriteLine(stringComparer.Compare(c, d));


hope that will help , let me know if there is an alternate way of doing this



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s