title: Sorting array of structs on multiple elements in c#
body: <p>I have an array of structures.  The structure of my structs is this:</p>
  
  <pre><code>    [StructLayout(LayoutKind.Sequential)]
      public struct TOCRRESULTSHEADER
      {
          public int StructId;
          public int XPixelsPerInch;
          public int YPixelsPerInch;
          public int NumItems;
          public float MeanConfidence;
      }
  
      [StructLayout(LayoutKind.Sequential)]
      public struct TOCRRESULTSITEM
      {
          public short StructId;
          public short OCRCha;
          public float Confidence;
          public short X;
          public short Y;
          public short Width;
          public short Height;
      }
  
      [StructLayout(LayoutKind.Sequential)]
      public struct TOCRRESULTS
      {
          public TOCRRESULTSHEADER Hdr;
          public TOCRRESULTSITEM[] Item;
      }
  </code></pre>
  
  <p>I am populating a structure of type TOCRRESULTS like this:</p>
  
  <pre><code>  TOCRRESULTS MyArray = GetOCRForImage(filename);
  </code></pre>
  
  <p>I am sorting the array by the Y values just fine using this:</p>
  
  <pre><code> Array.Sort&lt;TOCRRESULTSITEM&gt;(MyArray.Item, (a,b) =&gt; a.Y.CompareTo(b.Y));
  </code></pre>
  
  <p>Is there a way to sort by Y and X without having to write my own sorting routine?</p>
  
  <p>I tried using LINQ:</p>
  
  <pre><code> var newarray = OCRLetterArray.Item.OrderBy(x =&gt; x.Y).ThenBy(x =&gt; x.X).ToArray();
  </code></pre>
  
  <p>but it never sorted my array.</p>
  
  <p>I apologize for the drastic change from my original posting.  I was hoping I could get away with a simple example.</p>