title: C# Linq Custom Sort
body: <p>I have a query in linqtosql that returns a LabelNumber:</p>
  
  <pre><code>var q = from list in db.Lists
          select list.LabelNumber;
  </code></pre>
  
  <p><code>var q</code> then becomes an <code>IEnumerable&lt;string&gt;</code> with elements like this:</p>
  
  <pre><code>{"1","2","2.A","2.B","3","3.A","3.B"}
  </code></pre>
  
  <p>I basically want to order the elements as they appear above, but I can't use the <code>OrderBy(x=&gt;x.LabelNumber)</code> because <code>"10"</code> would get placed after <code>"1"</code> and before <code>"2"</code>.  </p>
  
  <p>I assume I have to write a custom comparator function, but how do I do this with linq?</p>
  
  <p>Edit: I think all of the answers below will work, but one caveat must be added to all responses.</p>
  
  <p>If you are using Linq2SQL you cannot use array indexes within the query.  To overcome this, you should have two queries.  One that reads from SQL.  The second does the ordering:</p>
  
  <pre><code>var q = from list in db.Lists
              select list.LabelNumber;
  
  var q2 = q.AsEnumerable()
            .OrderBy(x =&gt; int.Parse(x.LabelNumber.Split('.')[0]))
            .ThenBy(x =&gt; x.Number
                          .Contains(".") ? 
                                x.LabelNumber.Split('.')[1].ToString() 
                                : 
                                string.Empty);
  </code></pre>