Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Generic Multi-Field/Property Sorting for Lists of Business Objects

, 13 Feb 2008
This article presents a simple and flexible way to sort strongly-typed lists of business objects using multiple properties or fields.
multisortsourceanddemo.zip
MultiSortLib
bin
Debug
MultiSortLib.dll
Release
MultiSortLib.instr.pdb
Properties
HyperPropertyDescriptor
HyperPropertyDescriptor
bin
Release
HyperPropertyDescriptor.csproj.user
Properties
HyperPropertyDescriptorSample
bin
Release
HyperPropertyDescriptor.dll
HyperPropertyDescriptor.pdb
HyperPropertyDescriptorSample.exe
HyperPropertyDescriptorSample.pdb
HyperPropertyDescriptorSample.csproj.user
Properties
MultiSortDemo
bin
Debug
MultiSortDemo.exe
MultiSortDemo.vshost.exe
MultiSortLib.dll
Release
MultiSortDemo.instr.pdb
MultiSortDemo.vshost.exe
MultiSortLib.instr.pdb
Properties
DataSources
WorkItem.datasource
WorkItemPropertyName.datasource
Settings.settings
using System;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;

// Source code by Owen Emlen (owene_1998@yahoo.com, owen@binarynorthwest.com)

namespace BinaryNorthwest
{
    /// <summary>
    /// Stores a property or field name that will be used to determine sort order.
    /// Also contains a flag, fSortDescending, for specifying descending sort order.
    /// </summary>
    public class SortPropOrFieldAndDirection
    {
        #region "Constructors"
        public SortPropOrFieldAndDirection() { }

        public SortPropOrFieldAndDirection(string sPropOrFieldNameToSort)
        {
            sPropertyOrFieldName = sPropOrFieldNameToSort;
        }

        public SortPropOrFieldAndDirection(string sPropOrFieldNameToSort, bool fDescendingSort)
        {
            sPropertyOrFieldName = sPropOrFieldNameToSort;
            fSortDescending = fDescendingSort;
        }

        public SortPropOrFieldAndDirection(string sPropOrFieldNameToSort, SortType sortTyp)
        {
            sPropertyOrFieldName = sPropOrFieldNameToSort;
            sortType = sortTyp;
        }

        public SortPropOrFieldAndDirection(string sPropOrFieldNameToSort, bool fDescendingSort, SortType sortTyp)
        {
            sPropertyOrFieldName = sPropOrFieldNameToSort;
            fSortDescending = fDescendingSort;
            sortType = sortTyp;
        }
        #endregion

        /// <summary>
        /// Retrieves a IComparer of type T, depending on whether the instance of this
        /// class (or a derived class) references a property or field
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IComparer<T> GetComparer<T>() where T : class
        {            
            if (NameIsPropertyName)
            {
                CompareProperties<T> comp = new CompareProperties<T>(sPropertyOrFieldName, fSortDescending, sortType);
                comp.pi = pi;
                comp.Initialize();
                return comp;
            }
            else
            {
                CompareFields<T> comp = new CompareFields<T>(sPropertyOrFieldName, fSortDescending, sortType);
                comp.fi = fi;
                comp.Initialize();
                return comp;
            }
        }

        #region "Fields and Properties"
        /// <summary>
        /// This virtual property is overridden in the SortPropertyAndDirection and SortFieldAndDirection
        /// (derived) classes.  It indicates whether the "sPropertyOrFieldName" field refers to a 
        /// property name or a field name.
        /// </summary>
        internal virtual bool NameIsPropertyName { get { return true; } }
        public string sPropertyOrFieldName;
        public bool fSortDescending;
        public SortType sortType = SortType.eUsePropertyOrFieldType;
        
        /// <summary>
        /// (Cached PropertyInfo or FieldInfo)
        /// These fields are made available to avoid repeat reflection (GetProperty/GetField) if the caller has 
        /// already obtained the PropertyInfo or FieldInfo class instances for the property/field
        /// </summary>
        public PropertyInfo pi;
        
        
        public PropertyDescriptor property;

        public FieldInfo fi;
        #endregion
    }

    /// <summary>
    /// A SortPropOrFieldAndDirection-derived class, SortPropertyAndDirection handles PropertyInfo caching and asc/desc logic
    /// </summary>
    public class SortPropertyAndDirection : SortPropOrFieldAndDirection
    {
        internal override bool NameIsPropertyName { get { return true; } }
        public SortPropertyAndDirection() : base() { }
        public SortPropertyAndDirection(string sPropOrFieldNameToSort) : base(sPropOrFieldNameToSort) { }
        public SortPropertyAndDirection(string sPropOrFieldNameToSort, bool fDescendingSort) : base(sPropOrFieldNameToSort, fDescendingSort) { }
        public SortPropertyAndDirection(string sPropOrFieldNameToSort, SortType sortTyp) : base(sPropOrFieldNameToSort, sortTyp) { }
        public SortPropertyAndDirection(string sPropOrFieldNameToSort, bool fDescendingSort, SortType sortTyp) : base(sPropOrFieldNameToSort, fDescendingSort, sortTyp) { }
    }

    /// <summary>
    /// A SortPropOrFieldAndDirection-derived class, SortFieldAndDirection handles FieldInfo caching and asc/desc logic
    /// </summary>
    public class SortFieldAndDirection : SortPropOrFieldAndDirection
    {
        internal override bool NameIsPropertyName { get { return false; } }
        public SortFieldAndDirection() : base() { }
        public SortFieldAndDirection(string sPropOrFieldNameToSort) : base(sPropOrFieldNameToSort) { }
        public SortFieldAndDirection(string sPropOrFieldNameToSort, bool fDescendingSort) : base(sPropOrFieldNameToSort, fDescendingSort) { }
        public SortFieldAndDirection(string sPropOrFieldNameToSort, SortType sortTyp) : base(sPropOrFieldNameToSort, sortTyp) { }
        public SortFieldAndDirection(string sPropOrFieldNameToSort, bool fDescendingSort, SortType sortTyp) : base(sPropOrFieldNameToSort, fDescendingSort, sortTyp) { }
    }
}

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)

About the Author

owen654321
Software Developer (Senior) Troppus Software
United States United States
Currently working as a Senior Silverlight Developer with Troppus Software in Superior, CO. I enjoy statistics, programming, new technology, playing the cello, and reading codeproject articles. Smile | :)

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 13 Feb 2008
Article Copyright 2007 by owen654321
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid