Click here to Skip to main content
Click here to Skip to main content

Simple PropertyComparer

, 1 Nov 2006
Rate this:
Please Sign up or sign in to vote.
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:

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:

    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)

Share

About the Author

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

Comments and Discussions

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

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

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

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