Tags

, , , , , , , , ,

Just a quick upload to explain the Sort method for IEnumerable objects that might help in many cases and which is very easy to implement. In this approach I have used Extension method , Linq operator and little bit of reflection. You can also do same by calling Array.Sort, or by List’s Sort method using IComparable implementation but as I say its just an alternative that I found very handy than the existing one.

Lets say we have a class Employee and an array of Employee class

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

    var employees = new Employee[]
                    {
                        new Employee(){Name = "Navnath1"},
                        new Employee(){Name = "Navnath2"},
                        new Employee(){Name = "Navnath3"},
                        new Employee(){Name = "Navnath4"},
                        new Employee(){Name = "Navnath5"}
                    };

Thankfully .Net has Linq which does our job instantly, by using OrderBy  and OrderByDescending operaters. like

    employees.OrderBy(employee => employee.Name);

Iterating over this result will give our required result.

Lets make it more usable and simple. For this we will create extension method with Sort method that takes sort by name and direction of sorting.

    public static class IEnumerableExtension
    {       
        public static IEnumerable<TElement> Sort<TElement>(this  IEnumerable<TElement> source, string sortBy, SortDirection sortDirection = SortDirection.Ascending)
        {
            if (source != null)
            {
                PropertyInfo fieldInfo = typeof(TElement).GetProperty(sortBy);

                if (fieldInfo != null)
                {
                    source = sortDirection == SortDirection.Ascending ?
                        source.OrderBy(emp => fieldInfo.GetValue(emp, null)) :
                        source.OrderByDescending(emp => fieldInfo.GetValue(emp, null));
                }
            }

            return source;
        }

        public static IEnumerable<TElement> Sort<TElement, TKey>(this IEnumerable<TElement> source, Func<TElement, TKey> sortByExpression, SortDirection sortDirection = SortDirection.Ascending)
        {
            if (source != null)
            {
                source = sortDirection == SortDirection.Ascending ?
                    source.OrderBy(sortByExpression) :
                    source.OrderByDescending(sortByExpression);
            }

            return source;
        }
    }

Thats it… Now you can simply call Sort on any object that implements IEnumerable  interface,

    employees.Sort("Name", SortDirection.Descending); //Using string  OR
    employees.Sort(emp => emp.Name, SortDirection.Ascending); //Using Expression  OR
    employees.Sort(emp => emp.Name); //Ascending is default

This approach can help specifically in ASP.Net where DataGrid, GridView needs custom sorting implementation using field name in string type and direction to sort on binding object.

Nothing much special but just a handy method to help sorting… 🙂

Advertisements