Introduction
A generic class, used for comparing an object by one of it's properites.
Background
I wanted a way to sort custom collections on their properties.
Using the code
This is a very basic implementation of a basic class person and a collection of person objects. In the sort method of the collection, a new instance of the base compare class is called. This method uses the field name (property name), the class type which is contained in the collection, and the direction for the sort. The base compare class uses reflection to find the property, and then retrieves the values of the properties and compares.
I find this comes in handy when you want to implement sorting on a datagrid which is data bound to a custom collection.
<CODE>
using System;
using System.Collections;
using System.Reflection;
public class BaseClassComparer : IComparer
{
#region Private Members
private int _dir =1;
private PropertyInfo propInfo;
#endregion
public BaseClassComparer( string key , Type objType , int direction )
{
setComparer( key, objType , direction );
}
public BaseClassComparer( string key, Type objType )
{
setComparer( key, objType ,1 );
}
#region Public Methods
public int Compare( object x , object y )
{
IComparable a = ( IComparable ) propInfo.GetValue( x , null );
return ( a.CompareTo( propInfo.GetValue( y , null ) ) * _dir );
}
#endregion
#region Private Methods
private void setComparer( string key , Type objType , int direction )
{
if ( direction.Equals( -1 ) )
_dir = -1;
propInfo = objType.GetProperty( key );
if ( propInfo == null )
throw new ArgumentException( "key" , "The property does not exist." );
}
#endregion
}
public class Person
{
private int _internalId = 0;
private string _firstName = string.Empty ;
private string _lastName = string.Empty;
private string _streetAddress = string.Empty;
private string _city = string.Empty;
private string _zip = string.Empty;
private string _state = string.Empty;
public Person () {}
public string firstName
{
get{ return _firstName; }
set{ _firstName = value; }
}
public string lastName
{
get{ return _lastName; }
set{ _lastName = value; }
}
public string streetAddress
{
get{ return _streetAddress; }
set{ _streetAddress = value; }
}
public string city
{
get{ return _city; }
set{ _city = value; }
}
public string zip
{
get{ return _zip; }
set{ _zip = value; }
}
public DateTime lastLoan
{
get{ return _lastLoan; }
set{ _lastLoan = value; }
}
public int internalId
{
get { return _internalId; }
set { _internalId = value; }
}
}
public class Persons : CollectionBase
{
public Person this[ int index ]
{
get
{
return( ( Person ) List[ index ] );
}
set
{
List[ index ] = value;
}
}
public int Add( Person obj )
{
return( List.Add( obj) );
}
public int IndexOf( Person obj )
{
return( List.IndexOf( obj ) );
}
public void Insert( int index , Person obj )
{
List.Insert( index, obj );
}
public void Remove( Person obj )
{
List.Remove( obj );
}
public bool Contains( Person obj )
{
return( List.Contains( obj ) );
}
protected override void OnValidate( Object obj )
{
if ( objDJ.GetType() !=new DJ().GetType() )
throw new ArgumentException( "Object must be a Person." , "value" );
}
public void sort( string field , int dir)
{
InnerList.Sort(
new BaseClassComparer( field , this[ 0 ].GetType() , dir ) );
}
}
Points of Interest
Will be much easier in 2.0 with the introduction of generics. I find this a handy solution for sorting datagrids bound to collections.
History
No updates as of now.