title: Sorting a C# List by multiple columns
body: <p>I have a question on sorting a list by multiple columns. In the following sample, even though I sorted to show <code>None, A First Description, B Second...</code>, the list still prints in the order it was inserted. </p>
  
  <pre><code>List&lt;MySample&gt; samples = new List&lt;MySample&gt;();
  
  samples.Add(new MySample { SortOrder = 1, Data = "A First Description", Description = "A First Description" });
  samples.Add(new MySample { SortOrder = 1, Data = "C Third Description", Description = "C Third Description" });
  samples.Add(new MySample { SortOrder = 1, Data = "B Second Description", Description = "B Second Description" });
  samples.Add(new MySample { SortOrder = 0, Data = "None", Description = "None" });
  samples.OrderBy(a =&gt; a.SortOrder).ThenBy(a =&gt; a.Description).ToList();
  
  foreach (var item in samples)
  {
      Console.WriteLine(item);
  }
  
  public class MySample
  {
      public int SortOrder { get; set; }
      public string Description { get; set; }
      public object Data { get; set; }
  }
  </code></pre>
  
  <p>If I change my code to do the following, then it prints in the desired order.</p>
  
  <pre><code>samples = samples.OrderBy(a =&gt; a.SortOrder).ThenBy(a =&gt; a.Description).ToList();
  </code></pre>
  
  <p>Can the ordering be done without assigning(like above)?</p>
  
  <p>FYI, this example is not actual code. I need to databind this list to a <code>ComboBox</code> in <code>WPF</code> through code behind using <code>FrameworkElementFactory</code>.</p>
  
  <p>Thanks for your help!</p>
  
  <p><strong>Update with my approach:</strong></p>
  
  <pre><code>var collectionView = CollectionViewSource.GetDefaultView(&lt;My list to be sorted&gt;);
  collectionView.SortDescriptions.Add(new SortDescription("SortOrder", ListSortDirection.Ascending));
  collectionView.SortDescriptions.Add(new SortDescription("Description", ListSortDirection.Ascending));
  </code></pre>
  
  <p>The above did the trick in the UI. I thank all for the fast answers. </p>