Click here to Skip to main content
13,149,073 members (58,063 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 1 Nov 2006

Simple PropertyComparer

, 1 Nov 2006
Rate this:
Please Sign up or sign in to vote.
A Simple PropertyComparer


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:

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);
                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);
                return Comparer.Default.Compare(valueY, valueX);


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

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.


  • 1st November, 2006: Initial post


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


About the Author

Ion Botnari
Web Developer
Moldova (Republic of) Moldova (Republic of)
No Biography provided

You may also be interested in...

Comments and Discussions

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170924.2 | Last Updated 1 Nov 2006
Article Copyright 2006 by Ion Botnari
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid