views:

233

answers:

3

This sound simple but it not that much.

I want to order a List based on one of the properties of T, which is double type.

+1  A: 
var list = (from t in list
            orderby t.doubleVal).ToList();
hypoxide
This is not a legal assignment if `list` is a `List<T>`; the return type of `Enumerable<T>.OrderBy` is `IEnumerable<T>`.
Jason
Thanks for the correction, my C# syntax knowledge is rusty as I mainly stick to VB.NET.
hypoxide
Now your answer does not sort the list, it produces an `IEnumerable<T>` that can be enumerated over to get an ordered version of the list.
Jason
+4  A: 

If you know the propertyname before compilation:

myList = myList.OrderBy(a=>a.propertyName).ToList();

or

myList = (from m in myList order by m.propertyName).ToList();

If you don't have the property at compile time (e.g. dynamic sorting in a grid or something); try the following extension methods:

static class OrderByExtender
{
    public static IOrderedEnumerable<T> OrderBy<T>(this IEnumerable<T> collection, string key, string direction)
    {
        LambdaExpression sortLambda = BuildLambda<T>(key);

        if(direction.ToUpper() == "ASC")
            return collection.OrderBy((Func<T, object>)sortLambda.Compile());
        else
            return collection.OrderByDescending((Func<T, object>)sortLambda.Compile());
    }

    public static IOrderedEnumerable<T> ThenBy<T>(this IOrderedEnumerable<T> collection, string key, string direction)
    {
        LambdaExpression sortLambda = BuildLambda<T>(key);

        if (direction.ToUpper() == "ASC")
            return collection.ThenBy((Func<T, object>)sortLambda.Compile());
        else
            return collection.ThenByDescending((Func<T, object>)sortLambda.Compile());
    }

    private static LambdaExpression BuildLambda<T>(string key)
    {
        ParameterExpression TParameterExpression = Expression.Parameter(typeof(T), "p");
        LambdaExpression sortLambda = Expression.Lambda(Expression.Convert(Expression.Property(TParameterExpression, key), typeof(object)), TParameterExpression);
        return sortLambda;
    }
}

Then order like

myList = myList.OrderBy("propertyName", "ASC").ToList();
Jan Jongboom
I know the property name, and that what I immediately was on my mind, but the problem that I don't have OrderBy function!
Mendy
As long as you have a `using System.Linq` statement on top of your class, this'd work.
Jan Jongboom
Yep, it seems that I missed the reference. thanks.
Mendy
A: 

I think this sould do the trick:

List<T> list = new List<T>();
//fill list here    
list.OrderBy(item => item.DoubleTypeProperty).ToList();

HTH

Pavel Nikolov
Forgot the `ToList()`.
Jan Jongboom
This does not sort the list; it produces an `IEnumerable<T>` that can be enumerated over to get an ordered version of the list.
Jason
@Jan - It depends what you want to do with the result.
Pavel Nikolov
You didn't do anything with the result.
Jason
@Jason - I know what it does - just wanted to give a hint...
Pavel Nikolov