A simple example, then, may be:
public class BlogEntry
{
private String text;
public BlogEntry()
}
public string Text
get{return text;}
set{text = value;}
Using DataView.Sort its very easy to use code already in the runtime to sort DataRows. With Custom Types however there is no readily apparent built in solution. Dig slightly beneath the surface and you will find the IComparer interface and the ArrayList.Sort(IComparer). This, then, is one solution, but how can you implement this model without very large if/then statements that are specific to a Type? I would like to:
using System;
using System.Collections;
using System.Reflection;
namespace damonpaynedotcom.Types
/// <summary>
/// Make it easy to Sort Custom Types
/// </summary>
public class TypeComparer : IComparer
private delegate int DoCompare(object x, object y);
private DoCompare _compareMethod;
private PropertyInfo _propInfo;
private Hashtable _methods;
private bool _caseSensitive;
private string _sortProperty;
public TypeComparer(string propertyName, Type objectType)
_caseSensitive = true;
_sortProperty = propertyName;
_methods = BuildSortMethods();
_propInfo = objectType.GetProperty(_sortProperty);
if (null == _propInfo)
throw new ArgumentException(string.Format("Type {0} does not have a Property '{1}'", new Object[] {objectType.ToString(), propertyName}));
_compareMethod = (DoCompare)_methods[_propInfo.PropertyType];
if (null == _compareMethod)
throw new ArgumentException( string.Format("Type not supported: {0}", _propInfo.PropertyType) );
protected Hashtable BuildSortMethods()
Hashtable ht = new Hashtable();
ht.Add(typeof(int), new DoCompare(CompareInt));
ht.Add(typeof(double), new DoCompare(CompareDouble));
ht.Add(typeof(string), new DoCompare(CompareString));
ht.Add(typeof(DateTime), new DoCompare(CompareDate));
return ht;
public bool CaseSensitive
get{return _caseSensitive;}
set{_caseSensitive = value;}
protected int CompareInt(object x, object y)
int xInt = (int)x;
int yInt = (int)y;
return xInt.CompareTo(yInt);
protected int CompareDouble(object x, object y)
double xDouble = (double)x;
double yDouble = (double)y;
return xDouble.CompareTo(yDouble);
protected int CompareDate(object x, object y)
DateTime xDate = (DateTime)x;
DateTime yDate = (DateTime)y;
return xDate.CompareTo(yDate);
protected int CompareString(object x, object y)
string xString = (string)x;
string yString = (string)y;
if (!CaseSensitive)
xString = xString.ToUpper();
yString = yString.ToUpper();
return xString.CompareTo(yString);
#region IComparer Members
public int Compare(object x, object y)
object xVal = _propInfo.GetValue(x, null);
object yVal = _propInfo.GetValue(y, null);
return _compareMethod(xVal, yVal);
#endregion