Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Custom Generic Compare For Collection

0.00/5 (No votes)
20 Dec 2006 1  
A generic class, used for comparing an object by one of it's properites.

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;


    //

    // This is a generic class, used for comparing an object by one of it's properites

    //

    public class BaseClassComparer : IComparer
    {
        #region Private Members
        
        private int _dir =1;
        private PropertyInfo propInfo;
        
        #endregion
        //

        // This is a generic class, used for comparing an object by one of it's properites.

        // will throw ArgumentException property is not part of object.

        //

        // key = This is the name of the properites to sort by

        // objType = Type of object compared>

        // direction =1 for ascending (default) -1 for descending

        
        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
    }
    
    // Basic Person object    

    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; }
        }
    }

    // Basic Collection of Person objects    

    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)
        {
            // Use of the BaseClassComparer

            // key parm(1) - This is the name of the properites to sort by

            // objType parm(2) - Type of object compared

            // direction parm(3) - 1 for ascending (default) -1 for descending

            
            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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here