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

Generic Sorting with LINQ and Lambda Expressions

By , 24 Jun 2009
 

Introduction

Sorting objects has been a programming problem that has gone from a science to a mere few lines of code. Though it does not take nearly the amount of consideration as it did before my time in the industry, it's still just as relevant. This article takes a look at sorting using Lambda Expressions and Generics. In my opinion, the best technique for sorting objects that I have seen so far. This class has found a definite home in my Utils assembly, and so I share it with you.

Background

As programmers, it is always our duty, and pleasure, to find better ways to the same thing. That is how I stumbled on this sorting technique. I was working on a project that had several grids that required paging and sorting, and like many projects, we were using an object model. I was thinking that I wanted a generic sorting class that did all the work in one place, and this article shares the results.

Using the code

These samples have been dumped down a little from my actual implementation to improve readability for the purposes of this article. After reviewing this code though, I am confident that you will be able to think of several slick uses for this technique like I have.

Usage of the sorting class

C#:

GenericSorter<surveystateformatdata> gs = new GenericSorter<surveystateformatdata >();
SurveyStateFormatItems = gs.Sort(SurveyStateFormatItems.AsQueryable, 
                                 sortExpression, sortDirection).ToArray();

VB.NET:

Dim gs As New GenericSorter(Of SurveyStateFormatData)
SurveyStateFormatItems = gs.Sort(SurveyStateFormatItems.AsQueryable, _
                                 sortExpression, sortDirection).ToArray()

Here is the sorting class:

C#:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Linq.Expressions; 


public class GenericSorter<T>
{
    public IEnumerable<T> Sort(IEnumerable<T> source, string sortBy, string sortDirection)
    {
        var param = Expression.Parameter(typeof(T), "item");

        var sortExpression = Expression.Lambda<Func<T, object>>
            (Expression.Convert(Expression.Property(param, sortBy), typeof(object)), param);

        switch (sortDirection.ToLower())
        {
            case "asc":
                return source.AsQueryable<T>().OrderBy<T, object>(sortExpression);
            default:
                return source.AsQueryable<T>().OrderByDescending<T, object>(sortExpression);

        } 
    }
}

VB.NET:

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Linq.Expressions

Public Class GenericSorter(Of T)

    Public Function Sort(ByVal source As IEnumerable(Of T), _
                         ByVal sortBy As String, _
                         ByVal sortDirection As String) As IEnumerable(Of T)

        Dim param = Expression.Parameter(GetType(T), "item")

        Dim sortExpression = Expression.Lambda(Of Func(Of T, Object))_
        (Expression.Convert(Expression.[Property](param, sortBy), _
        GetType(Object)), param)

        Select Case sortDirection.ToLower
            Case "asc"
                Return source.AsQueryable().OrderBy(sortExpression)
            Case Else
                Return source.AsQueryable().OrderByDescending(sortExpression)
        End Select

    End Function

End Class

History

  • Article added: (06/22/2009).

License

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

About the Author

AdamNThompson
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to sort a Collection base on a given dynamic sort field string listmemberMember 412087022 Nov '12 - 15:18 
Dear Adam
Could you please help me to solve my problem that I have a screen which allow user choose a list of sort fields and for each field user also choose it descending or increasing
To do that feature, I try to code a function like this:
 
public IEmumerable Sort (IEnumerable unsortCollection, string[] sortField, int[] direction)
{
.....
}
 
But I don't know how to do it
 
Could you please so me the way to do
 
Many thanks
AnswerRe: How to sort a Collection base on a given dynamic sort field string listmemberAdamNThompson3 Dec '12 - 4:05 
a collection can be sorted multiple times. Just call the sort method for each field the user selects in the UI.
-Adam N. Thompson
adam-thompson.com

QuestionPerformancememberrickoshay29 Jun '09 - 9:39 
Have you done any performance testing? How would it compare to other sorting techniques?
AnswerRe: PerformancememberAdamNThompson30 Jun '09 - 8:27 
I am using the code in production and am not currently having any issues with performance. However, I am making use of reflection to get the type of the object which can have some performance implications. I know that I can use it to sort in the neighborhood of 500 large objects faster than I can blink, so I haven’t been at all concerned as the app I am using it in will never have that many objects to display in a GridView. If you however have more than that you may want to consider performance testing before implementing this technique.
 
-Adam N. Thompson

GeneralThis is a neat concept, but example does not compile.memberMichael Lee Yohe23 Jun '09 - 9:21 
According to VS2008 SP1, type inference is not implied by the usage of:
 
return source.AsQueryable().OrderBy(sortExpression);
 
Any thoughts?
GeneralRe: This is a neat concept, but example does not compile.memberAdamNThompson23 Jun '09 - 11:20 
I created the C# example with a code converter as the project I developed this in was in VB.NET. I will run sime test and update the article. But I think it might should be something like this in C#.
 
return source.AsQueryable<T>().OrderBy<T, object>(sortExpression);

 
-Adam N. Thompson

GeneralFigured it out with some modifications to the C# version:memberMichael Lee Yohe23 Jun '09 - 11:43 
Adam,
 
Your little demonstration is something I've been looking to do properly with Linq for some time now. Thank you for explaining about the C# converter part, as I thought you may be using C# 4.0 instead. I converted the process to an extension method so that no class is required to be instantiated for its use (you are welcome to incorporate it in your article):
 
   /// <summary>
   /// The direction that we are to sort an IEnumerable collection.
   /// </summary>
   public enum SortDirection
   {
      /// <summary>
      /// Sort ascending.
      /// </summary>
      Ascending,
      /// <summary>
      /// Sort descending.
      /// </summary>
      Descending
   }
 
      /// <summary>
      /// Extension method to sort dynamically based on a attribute referenced by a string.
      /// </summary>
      /// <typeparam name="T">The type of the IEnumerable collection we are working with.</typeparam>
      /// <param name="source">The source collection.</param>
      /// <param name="attribute">The name of the attribute.</param>
      /// <param name="sortDirection">The direction to perform the sort.</param>
      public static IEnumerable<T> Sort<T>(this IEnumerable<T> source, string attribute, SortDirection sortDirection)
      {
         var param = Expression.Parameter(typeof(T), "item");
 
         var sortExpression = Expression.Lambda<Func<T, object>>
         (
            Expression.Convert
            (
               Expression.Property(param, attribute),
               typeof(object)
            ),
            param
         );
 
         switch (sortDirection)
         {
            case SortDirection.Ascending:
            {
               return source.AsQueryable().OrderBy<T, object>(sortExpression);
            }
 
            default:
            {
               return source.AsQueryable().OrderByDescending<T, object>(sortExpression);
            }
         } 
      }
 
The usage of the extension method is simple:
 
class MyObject
{
   public int Value { get; set; }
}
 
List<MyObject> list = new List<MyObject>();
 
list.Add(new MyObject(50));
list.Add(new MyObject(20));
list.Add(new MyObject(60));
list.Add(new MyObject(20));
list.Add(new MyObject(10));
list.Add(new MyObject(40));
list.Add(new MyObject(30));
 
var sorted = list.Sort("Value", SortDirection.Ascending);
 
Now, I need to figure out how to adapt your method to be able to sort on multiple attributes in a given class. Then it would be absolutely perfect.
 
Thank you, again.
GeneralRe: Figured it out with some modifications to the C# version:memberAdamNThompson23 Jun '09 - 12:01 
"Now, I need to figure out how to adapt your method to be able to sort on multiple attributes in a given class. Then it would be absolutely perfect."
 
Let me know if you get that to work.
 
-Adam N. Thompson

GeneralRe: Figured it out with some modifications to the C# version:memberMichael Lee Yohe24 Jun '09 - 4:45 
Until I can figure out what how to define the Linq expression, this extension method (that uses my extension method) should do the trick.
 
      /// <summary>
      /// Extension method to sort dynamically based on a attribute referenced by a string.
      /// </summary>
      /// <typeparam name="T">The type of the IEnumerable collection we are working with.</typeparam>
      /// <param name="source">The source collection.</param>
      /// <param name="attributes">A dictionary containing the attributes and sort directions.</param>
      /// <remarks>Suitable for small collections.</remarks>
      public static IEnumerable<T> Sort<T>(this IEnumerable<T> source, Dictionary<string, SortDirection> attributes)
      {
         // result (initialize with the source)
         IEnumerable<T> result = source;
         
         // precedence is defined by the order of the dictionary
         foreach (string attribute in attributes.Keys)
         {
            result = result.Sort(attribute, attributes[attribute]);
         }
 
         return result;
      }
 
Or maybe it just sorts, and unsorts...? Have to research it more in depth.
GeneralRe: Figured it out with some modifications to the C# version:memberrxm020325 Jun '09 - 7:07 
You can use ThenBy after OrderBy for multiple attributes. Here is a link to the documentation.
http://msdn.microsoft.com/en-us/library/system.linq.queryable.thenby.aspx[^]
GeneralRe: Figured it out with some modifications to the C# version: [modified]memberSchmuli30 Jun '09 - 0:14 
What I did was a hack:
I return an IOrderedQueryable<T> from the first sort, and then overloaded the sort method to receive an IOrderedQueryable<T>. In such a case I perform a ThenBy<T, TResult> instead of the OrderBy<T, TResult>.
I guess once I do that I should call the sort method 'OrderBy' and the second sort 'ThenBy' so the intention is clear. Then after looking at the code, I realized that what I had was very similar to what exists in the framework, at least method names.
So I did the following:
 
These are the Extension methods:
public static class Extensions
{
    private enum SortDirection
    {
        Ascending,
        Descending,
    }
 
    public static IOrderedQueryable<T> OrderBy<T>( this IEnumerable<T> source, string sortBy )
    {
        return source.OrderBy( sortBy, SortDirection.Ascending );
    }
 
    public static IOrderedQueryable<T> OrderByDecending<T>( this IEnumerable<T> source, string sortBy )
    {
        return source.OrderBy( sortBy, SortDirection.Descending );
    }
 
    private static IOrderedQueryable<T> OrderBy<T>( this IEnumerable<T> source, string sortBy, SortDirection sortDirection )
    {
        var sortExpression = GetExpression<T>( sortBy );
 
        switch( sortDirection )
        {
            case SortDirection.Ascending:
                return source.AsQueryable<T>().OrderBy<T, object>( sortExpression );
 
            default:
                return source.AsQueryable<T>().OrderByDescending<T, object>( sortExpression );
 
        }
    }
 
    public static IOrderedQueryable<T> ThenBy<T>( this IOrderedQueryable<T> source, string sortBy )
    {
        return source.ThenBy( sortBy, SortDirection.Ascending );
    }
 
    public static IOrderedQueryable<T> ThenByDescending<T>( this IOrderedQueryable<T> source, string sortBy )
    {
        return source.ThenBy( sortBy, SortDirection.Descending );
    }
 
    private static IOrderedQueryable<T> ThenBy<T>( this IOrderedQueryable<T> source, string sortBy, SortDirection sortDirection )
    {
        var sortExpression = GetExpression<T>( sortBy );
 
        switch( sortDirection )
        {
            case SortDirection.Ascending:
                return source.ThenBy<T, object>( sortExpression );
 
            default:
                return source.ThenByDescending<T, object>( sortExpression );
 
        }
    }
 
    private static Expression<Func<T, object>> GetExpression<T>( string sortBy )
    {
        var param = Expression.Parameter( typeof( T ), "item" );
 
        return Expression.Lambda<Func<T, object>>( Expression.Convert( Expression.Property( param, sortBy ), typeof( object ) ), param );
    }
}
 
 
This is an example object with two properties:
public class MyObject
{
    public string Value
    {
        get;
        set;
    }
 
    public int Secondary
    {
        get;
        set;
    }
}
 
This is a usage example:
List<MyObject> array = new List<MyObject>
{
    new MyObject{ Value = "1", Secondary = 1 },
    new MyObject{ Value = "2", Secondary = 2 },
    new MyObject{ Value = "2", Secondary = 1 },
    new MyObject{ Value = "3", Secondary = 1 },
};
 
IEnumerable<MyString> sorted = array.OrderBy( "Value" ).ThenBy( "Secondary" );
 
foreach( var item in sorted )
{
    Console.WriteLine( item.Value + " " + item.Secondary );
}

 
modified on Tuesday, June 30, 2009 6:20 AM

GeneralRe: Figured it out with some modifications to the C# version:memberSchmuli30 Jun '09 - 0:55 
I know I'm replying to my own message, but if someone reads that one, perhaps they will read this one too.
Schmuli wrote:
var sorted = array.OrderBy( "Value" ).ThenBy( "Secondary" );

 
After having written my implementation and posted it here, I started to look over what I had. I basically had what was available to start with in System.Linq.Enumerable/Queryable, however, instead of using type-safety, by way of a delegate, to get the property to sort on, I was instead using strings, requiring also Reflection.
So I thought to myself, why would anyone want to implement sorting in this manner, and slowly it dawned on me that this is required when working with a GridView/DataGridView (as mentioned by the article's author). However, when using a GridView/DataGridView, although the sort expression is a string, the sort direction is also a string, so this means that having two methods for asc and desc will not work.
Furthermore, if I'm not mistaken, when sorting with a GridView/DataGridView, you only ever receive one sort expression to sort on. This means you need to keep some state in order to know which method to call, OrderBy or ThenBy.
All in all, although my code may come in handy for someone, it doesn't really answer the needs of the article's author.
 
Please let me know if I'm right in my assumptions, or does this still help with the original issue?
GeneralRe: Figured it out with some modifications to the C# version:memberAdamNThompson30 Jun '09 - 8:37 
It is true that I came up with this sorting method with the Gridview sorting in mind. Sometimes clients will impart business rules on you last minute though rules like, column x will always be the secondary sort.
 
I think your example is helpful in the exploration of this technique.
 
-Adam N. Thompson

GeneralRe: Figured it out with some modifications to the C# version:memberpavilp15 Jun '10 - 2:03 
Hi Adam,
I really like the technique you've shared and am using it in one of my projects. I came upon another instance where I wanted to reuse the same extension method and found a limitation I wasn't sure how to overcome. I have 2 classes defined as follows:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public Address Address { get; set; }
}
 

public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
I have an IList that I bind the Gridview to, however when I try to sort on any of the properties of the Address class it fails. Any ideas on how to sort on a complex property?
 
public static IEnumerable<T> Sort<T>(this IEnumerable<T> source, string sortExpression)
{
string[] sortParts = sortExpression.Split(' ');
var param = Expression.Parameter(typeof(T), string.Empty);
try
{
var property = Expression.Property(param, sortParts[0]); //This is where it fails when I try to sort on address
var sortLambda = Expression.Lambda<Func<T, object>>(Expression.Convert(property, typeof(object)), param);
 
if (sortParts.Length > 1 && sortParts[1].Equals(SortDirection.Descending.ToString(), StringComparison.OrdinalIgnoreCase))
{
return source.AsQueryable<T>().OrderByDescending<T, object>(sortLambda);
}
return source.AsQueryable<T>().OrderBy<T, object>(sortLambda);
}
catch (ArgumentException)
{
return source;
}
}
GeneralRe: Figured it out with some modifications to the C# version:memberAdamNThompson21 Jun '10 - 6:41 
Well... I'm not sure if this will work or not, but, the implementation uses reflection so...
 
Have you tried passing the property name into the sort extention method like "Person.Address.Street"?
 
Again, I haven't tried this, but give it a shot and let me know how it works out. If It dosen't work, maybe I will revisit the article to devise a solution.
 
Thanks,
-Adam N. Thompson

GeneralRe: Figured it out with some modifications to the C# version:membercakewalkr715 Apr '11 - 5:45 
I ran into the same issue as the previous poster regarding the sorting property that is actually another class. I tried fully qualifying the property such as below.
 
public class UserProject{
public int ProjectId {get;set;}
public Priorities Priority {get;set;}
}
 
public class Priority{
public int PriorityRank {get;set;}
}
 
I get a javascript error message when I try to sort on UserProject.Priority.PriorityRank that says "Instance property 'UserProject.Priority.PriorityRank' is not defined for type Objects.ListResults.UserProject". Were you ever able to figure out a way around this? Thanks!
GeneralRe: This is a neat concept, but example does not compile.memberAdamNThompson23 Jun '09 - 11:49 
Sorry. I alwayse forget that my angle brackets are stript out. Here is what the class should look like in C#. Thanks for letting me know, I have sent in an update for the article.
 
public class GenericSorter<T>
{
    public IEnumerable<T> Sort(IEnumerable<T> source, string sortBy, string sortDirection)
    {
        var param = Expression.Parameter(typeof(T), "item");
 
        var sortExpression = Expression.Lambda<Func<T, object>>
            (Expression.Convert(Expression.Property(param, sortBy), typeof(object)), param);
 
        switch (sortDirection.ToLower())
        {
            case "asc":
                return source.AsQueryable<T>().OrderBy<T, object>(sortExpression);
            default:
                return source.AsQueryable<T>().OrderByDescending<T, object>(sortExpression);
 
        } 
    }
}
 
Let me know how this works.
 
Smile | :)
 
-Adam N. Thompson

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 24 Jun 2009
Article Copyright 2009 by AdamNThompson
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid