8

I am attempting to write a generic sort extension method to List based on a string column name and linq.

I have most of it here, but it doesn't work just yet. The logic is taken from this site.

public static List<T> Sort<T>(this List<T> list, string sortColumn, string sortOrder) 
{
    if (string.IsNullOrWhiteSpace(sortColumn))
        return list;

    int order = sortOrder == "desc" ? -1 : 1;

    var param = Expression.Parameter(typeof(T), "x");
    var sortExpression = Expression.Lambda<Func<T, object>>(Expression.Property(param, sortColumn), param);
    list = list.AsQueryable().OrderBy(sortExpression).ToList();

    return list;
}

Stepping through this code I see that list is sorted properly, but when returned, it has no effect on the list I passed in. I'm guessing AsQueryable or OrderBy is creating a new object in memory and I'm no longer pointing to the same reference. Does anyone have some tips on how to make this work appropriately, or barring that, another solution? Thanks!

2
  • 1
    You should just return list.AsQueryable().OrderBy(sortExpression).ToList();
    – ChrisF
    Jul 8, 2011 at 19:03
  • You realize that your expression won't work on intrinsic types? (int, DateTime, bool, etc)
    – Marc
    Jul 8, 2011 at 19:19

3 Answers 3

11

It's a different list. The ToList() call creates a new List<T> instance, entirely separate from the List<T> that was passed into the method. It will have the same items in it, probably in a different order, but the list itself is a different object.

By the way, dynamic linq already does this so you might want to check it out. You can start reading about it here.

2
  • Exactly. Which is as expected. Realistically you would want to pass in an IQueryable and return an IQueryable as well.
    – Keltex
    Jul 8, 2011 at 19:04
  • Yeah, this is essentially what I want. Thanks for the tip! Jul 8, 2011 at 19:39
6

Does anyone have some tips on how to make this work appropriately, or barring that, another solution?

If you want to modify the original list, use List<T>.Sort.

2

One of the basic ideas behind LINQ is that it does not modify the things its called on.

OrderBy() can be called on any IEnumerable<T>, even those that aren't backed by anything resembling a list (e.g. it's generated on the fly). And it would be really inconsistent if it did something like: “If the source is IList<T>, sorts it and returns the original reference. If it doesn't, creates a new sorted sequence.”

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.