title: How to use LINQ to order within groups
body: <p>is it possible (using LINQ preferably) to order a collection which has a natural grouping, within the groups themselves without upsetting the group order?</p>
  
  <p>Let me explain. I have a collection thus:</p>
  
  <pre><code>        List&lt;grpitem&gt; list = new List&lt;grpitem&gt;()
          {
               new grpitem() {groupCode="1", item="a"},
               new grpitem() {groupCode="1", item="b"},
               new grpitem() {groupCode="1", item="c"},
               new grpitem() {groupCode="2", item="a"},
               new grpitem() {groupCode="2", item="c"},
               new grpitem() {groupCode="2", item="b"},
               new grpitem() {groupCode="3", item="c"},
               new grpitem() {groupCode="3", item="b"},
               new grpitem() {groupCode="3", item="a"}
          };
  </code></pre>
  
  <p>The order in which this arrives (order of groupCode) is determined elsewhere. I need to rearrange the collection so that within each group, elements are ordered by the item value, viz:</p>
  
  <pre><code>             {groupCode="1", item="a"},
               {groupCode="1", item="b"},
               {groupCode="1", item="c"},
               {groupCode="2", item="a"},
               {groupCode="2", item="b"},
               {groupCode="2", item="c"},
               {groupCode="3", item="a"},
               {groupCode="3", item="b"},
               {groupCode="3", item="c"},
  </code></pre>
  
  <p>The collection could just as equally arrived in the groupCode order "2", "3", "1", or whatever. I must not disturb that - I just need to reorder the elements within each group.</p>
  
  <p>A LINQ query would be really neat! (I'm probably being dim but I haven't yet found one)</p>
  
  <p>I've tried this:</p>
  
  <pre><code>        var reOrdered = (from x in list
                            group x by x.groupCode into groups
                            select groups)
                           .OrderBy(i =&gt; from j in i select j.item)
                           .SelectMany(i =&gt; i);
  </code></pre>
  
  <p>but I get an error - "at least one object must IComparable", which puzzles me.</p>
  
  <p>Lastly, this must be C# in .Net3.5, no later.</p>