rendered paste bodyusing System;
using System.Collections.Generic;
using System.Reflection;
namespace JBLib.Collections
{
[Serializable]
public class ObjectComparer<T> : IComparer<T>
{
public ObjectComparer( params string[] fields )
: this( fields, new bool[fields.Length] ) { }
public ObjectComparer( string[] fields, bool[] descending )
{
Fields = fields;
Descending = descending;
}
protected string[] Fields;
protected bool[] Descending;
#region IComparer<T> Members
public int Compare( T x, T y )
{
Type typex = x.GetType();
Type typey = y.GetType();
for( int i = 0; i < Fields.Length; i++ )
{
PropertyInfo pix = typex.GetProperty( Fields[i] );
PropertyInfo piy = typey.GetProperty( Fields[i] );
IComparable pvalx = ( IComparable )pix.GetValue( x, null );
object pvaly = piy.GetValue( y, null );
int iResult = pvalx.CompareTo( pvaly );
if( iResult != 0 )
{
if( Descending[i] )
{
return -iResult;
}
else
{
return iResult;
}
}
}
return 0;
}
#endregion
}
}