Wednesday, November 21, 2007

Example of how to compare two objects for sorting.

The example below demonstrates how you can compare two objects to allow for sorting. This is also a good example of how you can use reflection.

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Collections.Generic;

using System.Reflection;

/// <summary>

/// Summary description for ObjectComparer

/// </summary>

public class ObjectComparer<T> : IComparer<T>

{



#region Properties


private string _direction = "ASC";


public string Direction

{

get { return _direction; }

set { _direction = value; }

}


private string _compareField;


public string CompareField

{

get { return _compareField; }

set { _compareField = value; }

}


#endregion


#region Constructors


public ObjectComparer(string compareField, string direction)

{

_compareField = compareField;

_direction = direction;

}


#endregion


#region IComparer<T> Members


public int Compare(T x, T y)

{

//Split the fields into an array. The field is divided by dots,

//like Employee.Person.FirstName

string[] fieldParts = _compareField.Split('.');


object compareValX = x;

object compareValY = y;


//get the value of each field in the list.

foreach (string field in fieldParts)

{

compareValX = GetPropertyValue(compareValX, field);

compareValY = GetPropertyValue(compareValY, field);

}


//compare the values of the last fields in the list. Assumes the field's

//type implements IComparable.

int compareValue = ((IComparable)compareValX).CompareTo(compareValY);


//negate the result if dircection is descending

if (_direction.ToUpper() == "DESC")

compareValue = -1 * compareValue;


return compareValue;

}


private object GetPropertyValue(object o, string property)

{

//using reflection, load the type and return the value of

//the given property. This will throw an exception if the

//property cannot be found.

PropertyInfo pi = o.GetType().GetProperty(property);

object val = pi.GetValue(o, null);

return val;

}


#endregion


#region IComparer<T> Members


int IComparer<T>.Compare(T x, T y)

{

//throw new Exception("The method or operation is not implemented.");


//Split the fields into an array. The field is divided by dots,

//like Employee.Person.FirstName

string[] fieldParts = _compareField.Split('.');


object compareValX = x;

object compareValY = y;


//get the value of each field in the list.

foreach (string field in fieldParts)

{

compareValX = GetPropertyValue(compareValX, field);

compareValY = GetPropertyValue(compareValY, field);

}


//compare the values of the last fields in the list. Assumes the field's

//type implements IComparable.

int compareValue = ((IComparable)compareValX).CompareTo(compareValY);


//negate the result if dircection is descending

if (_direction.ToUpper() == "DESC")

compareValue = -1 * compareValue;


return compareValue;

}


#endregion

}

0 comments: