Click here to Skip to main content
15,878,970 members
Articles / Programming Languages / Visual Basic 10

Generic Sorting with LINQ and Lambda Expressions

Rate me:
Please Sign up or sign in to vote.
4.38/5 (7 votes)
24 Jun 2009CPOL1 min read 110.7K   30   19
This class sorts objects using Generics and LINQ Lambda Expressions.

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#:

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

VB.NET:

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

Here is the sorting class:

C#:

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:

VB
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)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralFigured it out with some modifications to the C# version: Pin
Michael Lee Yohe23-Jun-09 11:43
Michael Lee Yohe23-Jun-09 11:43 
GeneralRe: Figured it out with some modifications to the C# version: Pin
AdamNThompson23-Jun-09 12:01
AdamNThompson23-Jun-09 12:01 
GeneralRe: Figured it out with some modifications to the C# version: Pin
Michael Lee Yohe24-Jun-09 4:45
Michael Lee Yohe24-Jun-09 4:45 
GeneralRe: Figured it out with some modifications to the C# version: Pin
rxm020325-Jun-09 7:07
rxm020325-Jun-09 7:07 
GeneralRe: Figured it out with some modifications to the C# version: [modified] Pin
Schmuli30-Jun-09 0:14
Schmuli30-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: Pin
Schmuli30-Jun-09 0:55
Schmuli30-Jun-09 0:55 
GeneralRe: Figured it out with some modifications to the C# version: Pin
AdamNThompson30-Jun-09 8:37
AdamNThompson30-Jun-09 8:37 
GeneralRe: Figured it out with some modifications to the C# version: Pin
pavilp15-Jun-10 2:03
pavilp15-Jun-10 2:03 
GeneralRe: Figured it out with some modifications to the C# version: Pin
AdamNThompson21-Jun-10 6:41
AdamNThompson21-Jun-10 6:41 
GeneralRe: Figured it out with some modifications to the C# version: Pin
cakewalkr715-Apr-11 5:45
cakewalkr715-Apr-11 5:45 
GeneralRe: This is a neat concept, but example does not compile. Pin
AdamNThompson23-Jun-09 11:49
AdamNThompson23-Jun-09 11:49 

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

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