Tips & Tricks: How to Sort a Collection by it’s Property Name?


Several times we need to sort a collection based on some property name e.g. we have an Employee collection of type Person. Person consists of EmpId, Name, Age etc. Now as an user, I need to sort the collection in ascending or descending order either by EmpId, Name or Age. So, how can I do this?

 

In this post, I will describe you a simple technique by which you will be easily able to sort the collection without writing the implementation for each logic. Read the complete article and at the end if you have any suggestions, please don’t forget to share. Feedbacks are always highly appreciated.

 

Before going to the actual implementation, let us create our base class i.e. Employee having some properties like EmpId and Name.

public class Employee
{
    public string Name { get; set; }
    public string EmpId { get; set; }
}

Now, it’s time to create a class which will implement the sorting algorithm. Let us create a class named “FieldSort” and implement the IComparer interface. Let us make it little bit generic. Hence create the class of generic type T and implement it from the generic IComparer interface type of T.

IComparer has a method called Compare(). Just implement it in your class. Now inside the Compare() method get the PropertyInfo by doing a reflection and get the value of the property of each comparable object as string. Use string.Compare() to compare between the values and return the comparison result as integer.

Here is the full implementation of the class:

using System;
using System.Collections.Generic;
using System.Reflection;

namespace SortLibrary
{
    public class FieldSort<T> : IComparer<T>
    {
        private string propertyName = string.Empty;
        private SortOrder sortOrder = SortOrder.ASC;
        private StringComparison stringComparisonMethod =
                                 StringComparison.CurrentCultureIgnoreCase;

        /// <summary>
        /// Initializes a new instance of the <see cref="FieldSort&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="sortOrder">The sort order.</param>
        /// <param name="compareMethod">The compare method.</param>
        public FieldSort(string propertyName,
                         SortOrder sortOrder = SortOrder.ASC,
                         StringComparison compareMethod =
                                          StringComparison.CurrentCultureIgnoreCase)
        {
            this.propertyName = propertyName;
            this.sortOrder = sortOrder;
            this.stringComparisonMethod = compareMethod;
        }

        /// <summary>
        /// Compares the specified x with specified y.
        /// </summary>
        /// <param name="x">The original object x.</param>
        /// <param name="y">The object y which will be compared.</param>
        /// <returns></returns>
        public int Compare(T x, T y)
        {
            PropertyInfo propertyInfo = x.GetType().GetProperty(propertyName);

            if (propertyInfo != null)
            {
                string xValue = propertyInfo.GetValue(x, null).ToString();
                string yValue = propertyInfo.GetValue(y, null).ToString();

                if (xValue != null && yValue != null)
                {
                    return (sortOrder == SortOrder.ASC)
                            ? string.Compare(xValue, yValue, stringComparisonMethod)
                            : string.Compare(yValue, xValue, stringComparisonMethod);
                }
            }
            return 0;
        }
    }
}

Now let us discuss how to call this sort in our application. If you have the list collection of your Employee informations, you need to create the instance of the class FieldSort of type Employee with the proper arguments and pass as a parameter to the method Sort() of the List collection. That’s it. Your collection will sort automatically based on the “propertyName” argument you passed in the FieldSort class instance. You don’t need to call the Compare() method. The IComparer interface will take care of it for you.

Let’s see the calling method:

EmployeeList.Sort(new FieldSort<Employee>("Name",
                                           SortOrder.DESC,
                                           StringComparison.CurrentCultureIgnoreCase));

Once called, you will see the collection sorted according to your algorithm.

Here’s the original collection used for this example:

image

Once sort is done, you will see the sorted collection (descending order):

image

So, go ahead and use this class wherever you need. Don’t forget to provide your feedbacks. These are always welcome.


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

7 comments

  1. Why not simply:

    EmployeeList.Sort((x, y) => string.Compare(x, y, StringComparison.CurrentCultureIgnoreCase);

    ?

    ReplyDelete
  2. Yes, using LINQ you can do this very easily.

    I thought to share this for the people who are new to .Net and want to learn how actually the comparison happens.

    ReplyDelete
  3. If you are doing this in an intensive manner (i.e. binding on a large list) be aware of the performance hit for the reflection. You may want to consider caching the property info as a means around this.

    Also be careful where you use it. Do to the reflection calls, this MAY require full trust. As always, YMMV.

    ReplyDelete
  4. Actually that's not linq, sort is an old method. Linq would simply be

    EmployeeList.OrderBy(x => x.Name);
    or
    EmployeeList.OrderByDescending(x => x.Name);

    ReplyDelete
  5. Really very good suggestions. Appreciate your feedbacks. Thank you guys.

    ReplyDelete

 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top