title: Sorting a table in asp.net MVC
body: <p>I was wondering how people were going about sorting a table in asp.net mvc?
  I've heard of javascript solutions that work pretty well with non-paged tables, such as jquery's table sorter, but i need a solution that will work with paged tables.</p>
  
  <p>The project I'm working on currently uses the following solution, but I find it very messy.</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public ActionResult Sort(string parameter)
  {  
  
   IEnumerable&lt;IProduct&gt; list;
  
   if (Session["Model"] != null)
    list = (IEnumerable&lt;IProduct&gt;)Session["Model"]).ToList&lt;IProduct&gt;();
   else
    list = _service.GetAll();
  
   if (Session["parameter"] == null &amp;&amp; Session["sortDirection"] == null)
   {
    //set the parameter and set the sort to desc
    Session["parameter"] = parameter;
    Session["sortDirection"] = "DESC";
   }
   else if (Session["parameter"] != null) //already set so not the first time
   {
    //same parameter sent
    if (Session["parameter"].ToString().Equals(parameter))
    {
     //check sort direction and reverse
     if (Session["sortDirection"].ToString().Equals("DESC"))
      Session["sortDirection"] = "ASC";
     else
      Session["sortDirection"] = "DESC";
    }
    else //different parameter sent
    {
     Session["sortDirection"] = "DESC";
     Session["parameter"] = parameter;
    }
   }
  
   if (Session["sortDirection"].CompareTo("ASC") == 0)
    list = Models.ContollerHelpers.SortingHelper.OrderBy(list.AsQueryable(), column);
   else
    list = Models.ContollerHelpers.SortingHelper.OrderByDescending(list.AsQueryable(), column);
  
   return View("Results", list.ToList);
  }
  </code></pre>
  
  <p><strong>Helper</strong></p>
  
  <pre><code>public class Helper()
  {
   private static IOrderedQueryable&lt;T&gt; OrderingHelper&lt;T&gt;(IQueryable&lt;T&gt; source, string propertyName, bool descending, bool anotherLevel)
   {
    ParameterExpression param = Expression.Parameter(typeof(T), string.Empty); // I don't care about some naming
    MemberExpression property = Expression.PropertyOrField(param, propertyName);
    LambdaExpression sort = Expression.Lambda(property, param);
  
    MethodCallExpression call = Expression.Call(
     typeof(Queryable),
     (!anotherLevel ? "OrderBy" : "ThenBy") + (descending ? "Descending" : string.Empty),
     new[] { typeof(T), property.Type },
     source.Expression,
     Expression.Quote(sort));
  
    return (IOrderedQueryable&lt;T&gt;)source.Provider.CreateQuery&lt;T&gt;(call);
   }
  
   public static IOrderedQueryable&lt;T&gt; OrderBy&lt;T&gt;(this IQueryable&lt;T&gt; source, string propertyName)
   {
    return OrderingHelper(source, propertyName, false, false);
   }
  
   public static IOrderedQueryable&lt;T&gt; OrderByDescending&lt;T&gt;(this IQueryable&lt;T&gt; source, string propertyName)
   {
    return OrderingHelper(source, propertyName, true, false);
   }
  
   public static IOrderedQueryable&lt;T&gt; ThenBy&lt;T&gt;(this IOrderedQueryable&lt;T&gt; source, string propertyName)
   {
    return OrderingHelper(source, propertyName, false, true);
   }
  
   public static IOrderedQueryable&lt;T&gt; ThenByDescending&lt;T&gt;(this IOrderedQueryable&lt;T&gt; source, string propertyName)
   {
    return OrderingHelper(source, propertyName, true, true);
   }
  }
  </code></pre>
  
  <p><strong>List View</strong></p>
  
  <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;IEnumerable&lt;Models.Interface.IProduct&gt;&gt;" %&gt;
  &lt;% Session["model"] = Model; %&gt;
   &lt;table&gt;
      &lt;tr&gt;
     &lt;th&gt;
      Edit Details
     &lt;/th&gt;
     &lt;th&gt;
      &lt;%=Html.ActionLink("Id","Sort",new {parameter ="Id"}) %&gt;
     &lt;/th&gt;
     &lt;th&gt;
      &lt;%=Html.ActionLink("Name", "Sort", new { parameter = "Name"})%&gt;
     &lt;/th&gt;
     &lt;th&gt;
      &lt;%=Html.ActionLink("Status", "Sort", new { parameter = "Status" })%&gt;
     &lt;/th&gt;
     &lt;th&gt;
      &lt;%=Html.ActionLink("Notes", "Sort", new { parameter = "Notes"})%&gt;
     &lt;/th&gt;
    &lt;/tr&gt;
    &lt;% foreach (var item in Model){ %&gt;
  
     &lt;tr&gt;
      &lt;td&gt;
       &lt;%= Html.ActionLink("Edit", "Edit", new {  id=item.Id }) %&gt; |
      &lt;/td&gt;
      &lt;td&gt;
       &lt;%= Html.Encode(item.Id) %&gt;
      &lt;/td&gt;
      &lt;td&gt;
       &lt;%= Html.Encode(item.Name) %&gt;
      &lt;/td&gt;
      &lt;td&gt;
       &lt;%= Html.Encode(item.Status) %&gt;
      &lt;/td&gt;
      &lt;td&gt;
       &lt;%= Html.Encode(item.Notes) %&gt;
      &lt;/td&gt; 
     &lt;/tr&gt;
  
    &lt;% } %&gt;   
      &lt;/table&gt;
  </code></pre>
  
  <p>Is this the only way of doing something like this?
  If anyone knows of a nicer way that doesn't involve having all of the records being loaded to a page at once then please link to examples.</p>