title: Better way to Sort a List by any property
body: <p>My method receives all DataTables parameters to sort table by column clicked. I call this method from controller of each page list.
  I'm looking for a better way to do this like a generic method for all types: <strong>string</strong>, <strong>int</strong>, <strong>decimal</strong>, <strong>double</strong>, <strong>bool</strong> (<strong>nullable</strong> or <strong>not</strong>). But I can't find it.</p>
  
  <p>My current code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public List&lt;T&gt; OrderingList&lt;T&gt;(List&lt;T&gt; list, DataTablesParam model)
  {
      var iColumn = model.Order.FirstOrDefault().Column;
      var property = typeof(T).GetProperty(model.Columns.ToArray()[iColumn].Data);
      var param = Expression.Parameter(typeof(T));
      var final = Expression.Property(param, property);
  
      var isDirAsc = model.Order.FirstOrDefault().Dir.Equals("asc");
  
      if (property.PropertyType == typeof(string))
      {
          var lambda = Expression.Lambda&lt;Func&lt;T, string&gt;&gt;(final, param).Compile();
          return isDirAsc ? list.OrderBy(lambda).ToList() : list.OrderByDescending(lambda).ToList();
      }
      else if (property.PropertyType == typeof(int))
      {
          var lambda = Expression.Lambda&lt;Func&lt;T, int&gt;&gt;(final, param).Compile();
          return isDirAsc ? list.OrderBy(lambda).ToList() : list.OrderByDescending(lambda).ToList();
      }
      else if (property.PropertyType == typeof(bool))
      {
          var lambda = Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(final, param).Compile();
          return isDirAsc ? list.OrderBy(lambda).ToList() : list.OrderByDescending(lambda).ToList();
      }
      else if (property.PropertyType == typeof(decimal))
      {
          var lambda = Expression.Lambda&lt;Func&lt;T, decimal&gt;&gt;(final, param).Compile();
          return isDirAsc ? list.OrderBy(lambda).ToList() : list.OrderByDescending(lambda).ToList();
      }
      else if (property.PropertyType == typeof(double))
      {
          var lambda = Expression.Lambda&lt;Func&lt;T, double&gt;&gt;(final, param).Compile();
          return isDirAsc ? list.OrderBy(lambda).ToList() : list.OrderByDescending(lambda).ToList();
      }
  
      return list;
  }
  </code></pre>
  
  <hr />
  
  <p>I want to do something like this: (But this code doesn't work)</p>
  
  <pre class="lang-cs prettyprint-override"><code>public List&lt;T&gt; OrderingList&lt;T&gt;(List&lt;T&gt; list, DataTablesParam model)
  {
      var iColumn = model.Order.FirstOrDefault().Column;
      var property = typeof(T).GetProperty(model.Columns.ToArray()[iColumn].Data);
      var param = Expression.Parameter(typeof(T));
      var final = Expression.Property(param, property);
  
      var isDirAsc = model.Order.FirstOrDefault().Dir.Equals("asc");
  
      var lambda = Expression.Lambda&lt;Func&lt;T, dynamic&gt;&gt;(final, param).Compile();
      return isDirAsc ? list.OrderBy(lambda).ToList() : list.OrderByDescending(lambda).ToList();
  }
  </code></pre>
  
  <hr />