Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / C#
Article

Simple PropertyComparer

Rate me:
Please Sign up or sign in to vote.
3.61/5 (7 votes)
1 Nov 2006CPOL 29.4K   14   3
A Simple PropertyComparer

Introduction

This article is about a simple PropertyComparer class that can be used to sort collections of objects by property of the objects in the collection:

C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web.UI.WebControls;

namespace yourNamespace
{
    public class PropertyComparer<T> : IComparer<T>
    {
        private PropertyInfo property;
        private SortDirection sortDirection;

        public PropertyComparer(string sortProperty, SortDirection sortDirection)
        {
            property = typeof (T).GetProperty(sortProperty);
            Utils.AssertIsNotNull(
                string.Format("Property {0} not found on type {1}", sortProperty,
                              typeof (T).FullName), sortProperty);
            this.sortDirection = sortDirection;
        }

        public int Compare(T x, T y)
        {
            object valueX = property.GetValue(x, null);
            object valueY = property.GetValue(y, null);
            
            if (sortDirection == SortDirection.Ascending)
            {
                return Comparer.Default.Compare(valueX, valueY);
            }
            else
            {
                return Comparer.Default.Compare(valueY, valueX);
            }
        }
    }
}

Usage

Here is an example of using the PropertyComparer to sort a collection of objects:

C#
public class ObjectInCollectionType
{
    private string text = Guid.NewGuid().ToString();

    public string Text
    {
        get { return text; }
        set { text = value; }
    }
}
public class TestPropertyComparer
{
    public static void Test()
    {
        List<ObjectInCollectionType> myList = new List<ObjectInCollectionType>();
        myList.Add(new ObjectInCollectionType());
        myList.Add(new ObjectInCollectionType());
        myList.Add(new ObjectInCollectionType());
        myList.Add(new ObjectInCollectionType());
        // Sorting the collection
        myList.Sort(new PropertyComparer<ObjectInCollectionType>
            ("Text", SortDirection.Ascending));
    }
}

Happy programming.

History

  • 1st November, 2006: Initial post

License

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


Written By
Web Developer
Moldova (Republic of) Moldova (Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralComparer.Compare() throws an exception if none of the arguments impements IComparable Pin
Theo Bebekis23-Jul-09 1:42
Theo Bebekis23-Jul-09 1:42 
GeneralGood idea, but there's a better way Pin
kma24815-Nov-06 13:40
kma24815-Nov-06 13:40 
GeneralRe: Good idea, but there's a better way Pin
Theo Bebekis23-Jul-09 1:36
Theo Bebekis23-Jul-09 1:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.