Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Dynamite: High Performace Dynamic Sorting Using Expressions

, 25 Sep 2008
Easy-to-use and high performance dynamic sorting of most type of sequences with SQL-like syntax, developed using System.Linq.Expression classes.
/*
 * Code from The Code Project Article "Generic List Sort Function" by  Bryan Sumter
 * http://www.codeproject.com/KB/cs/GenericSorter.aspx
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Reflection;

namespace Extensions
{
    public static class Extensions
    {
        public static void Sort<T>(this List<T> list, string sortExpression)
        {
            string[] sortExpressions = sortExpression.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            List<GenericComparer> comparers = new List<GenericComparer>();
            
            foreach (string sortExpress in sortExpressions)
            {
                string sortProperty = sortExpress.Trim().Split(' ')[0].Trim();
                string sortDirection = sortExpress.Trim().Split(' ')[1].Trim();

                Type type = typeof(T);
                PropertyInfo PropertyInfo = type.GetProperty(sortProperty);
                if (PropertyInfo == null)
                {
                    PropertyInfo[] props = type.GetProperties();
                    foreach (PropertyInfo info in props)
                    {
                        if (info.Name.ToString().ToLower() == sortProperty.ToLower())
                        {
                            PropertyInfo = info;
                            break;
                        }
                    }
                    if (PropertyInfo == null)
                    {
                        throw new Exception(String.Format("{0} is not a valid property of type: \"{1}\"", sortProperty,type.Name));
                    }
                }

                SortDirection SortDirection = SortDirection.Ascending;
                if (sortDirection.ToLower() == "asc" || sortDirection.ToLower() == "ascending")
                {
                    SortDirection = SortDirection.Ascending;
                }
                else if (sortDirection.ToLower() == "desc" || sortDirection.ToLower() == "descending")
                {
                    SortDirection = SortDirection.Descending;
                }
                else
                {
                    throw new Exception("Valid SortDirections are: asc, ascending, desc and descending");
                }

                comparers.Add(new GenericComparer { SortDirection = SortDirection, PropertyInfo = PropertyInfo, comparers = comparers });
            }
            list.Sort(comparers[0].Compare);
        }
    }

    public class GenericComparer
    {
        public List<GenericComparer> comparers { get; set; }
        int level = 0;

        public SortDirection SortDirection { get; set; }
        public PropertyInfo PropertyInfo { get; set; }

        public int Compare<T>(T t1, T t2)
        {
            int ret = 0;
            
            if (level >= comparers.Count)
                return 0;

            object t1Value = comparers[level].PropertyInfo.GetValue(t1, null);
            object t2Value = comparers[level].PropertyInfo.GetValue(t2, null);
            
            if (t1 == null || t1Value == null)
            {
                if (t2 == null || t2Value == null)
                {
                    ret = 0;
                }
                else
                {
                    ret = -1;
                }
            }
            else
            {
                if (t2 == null || t2Value == null)
                {
                    ret = 1;
                }
                else
                {
                    ret = ((IComparable)t1Value).CompareTo(((IComparable)t2Value));
                }
            }
            if (ret == 0)
            {
                level += 1;
                ret = Compare(t1, t2);
                level -= 1;
            }
            else
            {
                if (comparers[level].SortDirection == SortDirection.Descending)
                {
                    ret *= -1;
                }
            }
            return ret;
        }
    }

    public class ExampleUser
    {
        public DateTime Birthday { get; set; }
        public string Firstname { get; set; }
    }

    public class ExampleClass
    {
        public static void Example()
        {
            List<ExampleUser> userlist = new List<ExampleUser>();
            userlist.Add(new ExampleUser { Birthday = new DateTime(1988, 10, 1), Firstname = "Bryan" });
            userlist.Add(new ExampleUser { Birthday = new DateTime(1986, 11, 4), Firstname = "Michael" });
            userlist.Add(new ExampleUser { Birthday = new DateTime(1977, 2, 2), Firstname = "Arjan" });
            userlist.Add(new ExampleUser { Birthday = new DateTime(1990, 6, 13), Firstname = "Pieter" });
            userlist.Add(new ExampleUser { Birthday = new DateTime(1988, 10, 1), Firstname = "Ruben" });
            userlist.Add(new ExampleUser { Birthday = new DateTime(1987, 8, 21), Firstname = "Bastiaan" });
            userlist.Add(new ExampleUser { Birthday = new DateTime(1987, 8, 21), Firstname = "Pieter" });

            string unsorted = "Unsorted: " + Environment.NewLine;
            foreach (ExampleUser user in userlist)
            {
                unsorted += String.Format("{0} / {1} {2}", user.Birthday.ToString("dd-MM-yyyy"), user.Firstname, Environment.NewLine);
            }

            userlist.Sort("Firstname asc");
            string sorted1 = "Sorted by Firstname ascending: " + Environment.NewLine;
            foreach (ExampleUser user in userlist)
            {
                sorted1 += String.Format("{0} / {1} {2}", user.Birthday.ToString("dd-MM-yyyy"), user.Firstname, Environment.NewLine);
            }

            userlist.Sort("Firstname asc, Birthday desc");
            string sorted2 = "Sorted by Firstname ascending, Birtday descending: " + Environment.NewLine;
            foreach (ExampleUser user in userlist)
            {
                sorted2 += String.Format("{0} / {1} {2}", user.Birthday.ToString("dd-MM-yyyy"), user.Firstname, Environment.NewLine);
            }

            userlist.Sort("Birthday asc, Firstname asc");
            string sorted3 = "Sorted by Birthday ascending, Firstname ascending: " + Environment.NewLine;
            foreach (ExampleUser user in userlist)
            {
                sorted3 += String.Format("{0} / {1} {2}", user.Birthday.ToString("dd-MM-yyyy"), user.Firstname, Environment.NewLine);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Henrik Jonsson
Software Developer
Sweden Sweden
Henrik Jonsson is a Microsoft Professional Certified Windows Developer (MCPD) that currently works as an IT consultant in Västerås, Sweden.
 
Henrik has worked in several small and large software development projects in various roles such as architect, developer, CM and tester.
 
He regularly reads The Code Project articles to keep updated about .NET development and get new ideas. He has contributed with articles presenting some useful libraries for Undo/Redo, Dynamic Linq Sorting and a Silverlight 5 MultiBinding solution.

| Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 25 Sep 2008
Article Copyright 2008 by Henrik Jonsson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid