title: achieving a complex sort via Linq to Objects
body: <p>I've been asked to apply conditional sorting to a data set and I'm trying to figure out how to achieve this via LINQ. In this particular domain, purchase orders can be marked as primary or secondary.  The exact mechanism used to determine primary/secondary status is rather complex and not germane to the problem at hand.</p>
  
  <p>Consider the data set below.   </p>
  
  <pre><code>Purchase Order    Ship Date       Shipping Address     Total
  6                  1/16/2006       Tallahassee FL      500.45 
  19.1             2/25/2006       Milwaukee WI        255.69 
  5.1              4/11/2006       Chicago IL          199.99 
  8                  5/16/2006       Fresno CA           458.22 
  19                7/3/2006        Seattle WA          151.55
  5                   5/1/2006        Avery UT            788.52    
  5.2                 8/22/2006       Rice Lake MO        655.00 
  </code></pre>
  
  <p>Secondary POs are those with a decimal number and primary PO's are those with an integer number.  The requirement I'm dealing with stipulates that when a user chooses to sort on a given column, the sort should <strong>only</strong> be applied to primary POs. Secondary POs are ignored for the purposes of sorting, but should still be listed below their primary PO in ship date descending order. </p>
  
  <p>For example, let's say a user sorts on Shipping Address ascending.  The data would be sorted as follows.  Notice that if you ignore the secondary POs, the data is sorted by Address ascending (Avery, Fresno, Seattle, Tallahassee)</p>
  
  <pre><code>Purchase Order      Ship Date       Shipping Address     Total
  5                   5/1/2006        Avery UT            788.52  
  --5.2               8/22/2006       Rice Lake MO        655.00  
  --5.1                4/11/2006       Chicago IL          199.99 
  8                   5/16/2006       Fresno CA           458.22 
  19                  7/3/2006        Seattle WA          151.55
  --19.1               2/25/2006       Milwaukee WI        255.69 
  6                   1/16/2006       Tallahassee FL      500.45 
  </code></pre>
  
  <p>Is there a way to achieve the desired effect using the OrderBy extension method? Or am I stuck (better off) applying the sort to the two data sets independently and then merging into a single result set?</p>
  
  <pre><code>public IList&lt;PurchaseOrder&gt; ApplySort(bool sortAsc)
  {
      var primary = purchaseOrders.Where(po =&gt; po.IsPrimary)
                                  .OrderBy(po =&gt; po.ShippingAddress).ToList();
      var secondary = purchaseOrders.Where(po =&gt; !po.IsPrimary)
                                    .OrderByDescending(po =&gt; po.ShipDate).ToList();
      //merge 2 lists somehow so that secondary POs are inserted after their primary
  }
  </code></pre>