Click here to Skip to main content
15,886,518 members
Articles / Desktop Programming / Windows Forms

BSEtunes

Rate me:
Please Sign up or sign in to vote.
4.67/5 (11 votes)
24 Apr 2010CPOL4 min read 64.6K   4.3K   58  
BSEtunes is a MySQL based, full manageable, networkable single or multiuser jukebox application
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.ComponentModel;

namespace BSE.Platten.BO
{
    public class PropertyComparer<T> : IComparer<T>
    {
        #region FieldsPrivate

        private PropertyDescriptor m_propertyDescriptor;
        private ListSortDirection m_listSortDirection;

        #endregion

        #region MethodsPublic
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="prop">A PropertyDescriptor that specifies the property to sort on.</param>
        /// <param name="direction">One of the ListSortDirection values.</param>
        public PropertyComparer(PropertyDescriptor prop, ListSortDirection direction)
        {
            this.m_propertyDescriptor = prop;
            this.m_listSortDirection = direction;
        }

        #region IComparer<T>
        /// <summary>
        /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
        /// </summary>
        /// <param name="x">The first object to compare.</param>
        /// <param name="y">The second object to compare.</param>
        /// <returns></returns>
        public int Compare(T x, T y)
        {
            // Get property values
            object xValue = GetPropertyValue(x, this.m_propertyDescriptor.Name);
            object yValue = GetPropertyValue(y, this.m_propertyDescriptor.Name);

            // Determine sort order
            if (this.m_listSortDirection == ListSortDirection.Ascending)
            {
                return CompareAscending(xValue, yValue);
            }
            else
            {
                return CompareDescending(xValue, yValue);
            }
        }

        #endregion

        #endregion

        #region MethodsPrivate

        private static int CompareAscending(object x, object y)
        {
            int result;

            if ((x != null) && (y == null))
            {
                return 1;
            }
            if ((x == null) && (y != null))
            {
                return -1;
            }
            if ((x == null) && (y == null))
            {
                return 0;
            }
            // If values implement IComparer
            IComparable value = x as IComparable;
            if (value != null)
            {
                result = value.CompareTo(y);
            }
            else if (x.Equals(y))
            {
                result = 0;
            }
            else
            {
                result = x.ToString().CompareTo(y.ToString());
            }
            return result;
        }

        private static int CompareDescending(object x, object y)
        {
            // Return result adjusted for ascending or descending sort order ie
            // multiplied by 1 for ascending or -1 for descending
            return CompareAscending(x, y) * -1;
        }

        private static object GetPropertyValue(T value, string property)
        {
            // Get property
            PropertyInfo propertyInfo = value.GetType().GetProperty(property);
            // Return value
            return propertyInfo.GetValue(value, null);
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions