title: C# Sort array of objects by property name exists in the object
body: <p>The following code, sorts any array of objects by any property name (as string) exists in the object.</p>
  
  <p>Sorting array is done using sorting direction "asc" or "desc" too.</p>
  
  <p>This is the goal I want to apply.</p>
  
  <pre><code>    using System.Linq.Dynamic;
  
      /// &lt;summary&gt;
      /// This method sorts any array of objects.
      /// &lt;/summary&gt;
      /// &lt;param name="dataSource"&gt;Array of objects&lt;/param&gt;
      /// &lt;param name="propertyName"&gt;property name to sort with&lt;/param&gt;
      /// &lt;param name="sortDirection"&gt;"ASC" or "DESC"&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      private object[] SortArrayOfObjects(object[] dataSource, string propertyName, string sortDirection)
      {
          string sortExpression = string.Format("{0} {1}", propertyName, sortDirection);
          // sortExpression will be something like "FirstName DESC".
  
          // OrderBy method takes expression as string like "FirstName DESC".
          // OrderBy method exists in "System.Linq.Dynamic" dll.
          // Download it from www.nuget.org/packages/System.Linq.Dynamic/
          object[] arrSortedObjects = dataSource.OrderBy(sortExpression).ToArray();
  
          return arrSortedObjects;
      }
  </code></pre>