7

Let's say I have a class like this

public class model
{
    public int id{get;set;}
    public string name{get;set;}
    public string department{get;set;}
}

and I have a List of type model

List<model> modelList = List<model>();

How Can I sort the modelList by its column name with sort direction?

Ways that I tried:

public List<model> sortModelList(string columnName, SortDirection direction)
{
   //Method 1:
   //The below code was unable to sort by column and unable to set the sort direction
   return modelList.Sort();

   //Method 2:
   //The below code was unable to sort by the columnName parameter and unable to set the sort direction
   return modelList.OrderBy(a=>a.name)

  //What I can do in order to sort the list by "columnName" parameter and set the sort direction? (Ascending / Descending)
}
2
  • The simple answer is that you need a customer compare function. I'm sure that, very soon, someone will show you how.
    – david.pfx
    Mar 12, 2014 at 3:27
  • 1
    look at the post at this page: stackoverflow.com/questions/41244/…
    – Emad
    Mar 12, 2014 at 3:31

5 Answers 5

12

I think you're looking for the overload of Sort() that takes a comparison function .

For example:

modelList.Sort((m1, m2) => string.Compare(m1.name, m2.name));
// descending
modelList.Sort((m1, m2) => -string.Compare(m1.name, m2.name));

OrderBy has similar flexibility, but returns a new sequence which is sorted rather than modifying the original list. So, you could do:

var newList = modelList.OrderBy(m => m.name).ToList();
// descending
var newList = modelList.OrderByDescending(m => m.name).ToList();

To specify the property to be sorted by dynamically, consider the following code:

public void SortList<T>(List<T> list, string columnName, SortDirection direction)
{
    var property = typeof(T).GetProperty(columnName);
    var multiplier = direction == SortDirection.Descending ? -1 : 1;
    list.Sort((t1, t2) => {
        var col1 = property.GetValue(t1);
        var col2 = property.GetValue(t2);
        return multiplier * Comparer<object>.Default.Compare(col1, col2);
    });
}
3
  • But sometimes the list won't sort by name, i.e., sometimes may sort by id, and may sort by department, is there any way to do that using this code?? Mar 12, 2014 at 3:36
  • How do I use public void public void SortList<T> if i wanted to sort my List<Countries> countries for example? countries = SortList(countries, "columnName", SortDirection.Ascending); gives me error cannot convert void to Collections.Generic.List.
    – VDWWD
    Oct 13, 2016 at 18:57
  • @VDWWD it sounds like your list type is not List<T>. You could change your type to be List<T>, call ToList() to perform the conversion, or change SortList to take the type you are using. The last option requires that the type you are using have a sort function that takes a Comparison<T> or IComparer<T> Oct 14, 2016 at 22:02
11

If you need to pass column name as a parameter, then you would need to use reflection to do your comparison. You can do something like below:

modelList.OrderBy(a => a.GetType().GetProperty(columnName).GetValue(a, null));

Of course you would have to do proper null checks etc, which I am assuming you would be able to take care of.

2
  • Thanks, this was exactly what i was looking for :) Nov 1, 2018 at 11:23
  • LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object, System.Object[])' method, and this method cannot be translated into a store expression just so you know this completely removes the point of using iqueryable.
    – Niklas
    Sep 26, 2020 at 14:26
1

The correct way to do this is to write a customer comparison function.

Everything you need to know is found here: http://msdn.microsoft.com/en-us/library/w56d4y5z%28v=vs.110%29.aspx

Have a go, and come back if it still won't work.

1

This is where you can create a dynamic expression from property name and sort direction

Dynamic LINQ OrderBy on IEnumerable<T>

1

Step 1: Implement IComparer interface

public class SortByName : IComparer<Customer>
{
    public int Compare(Customer x, Customer y)
    {
        return x.Name.CompareTo(y.Name);
    }
}

Step 2: Pass an instance of the class that implements IComparer interface, as an argument to the Sort() method.

SortByName sortByName = new SortByName();
listModel.Sort(sortByName);

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.