title: Sorting mixed numbers and strings
body: <p>I have a list of strings that can contain a letter or a string representation of an int (max 2 digits).
  They need to be sorted either alphabetically or (when it is actually an int) on the numerical value it represents.</p>
  
  <p>Example:</p>
  
  <pre><code>IList&lt;string&gt; input = new List&lt;string&gt;()
      {"a", 1.ToString(), 2.ToString(), "b", 10.ToString()};
  
  input.OrderBy(s=&gt;s)
    // 1
    // 10
    // 2
    // a
    // b
  </code></pre>
  
  <p>What I would want is</p>
  
  <pre><code>  // 1
    // 2
    // 10
    // a
    // b
  </code></pre>
  
  <p>I have some idea involving formatting it with trying to parse it, then if it is a successfull tryparse to format it with my own custom stringformatter to make it have preceding zeros. I'm hoping for something more simple and performant.</p>
  
  <p><strong>Edit</strong><br>
  I ended up making an IComparer I dumped in my Utils library for later use.<br>
  While I was at it I threw doubles in the mix too.</p>
  
  <pre><code>public class MixedNumbersAndStringsComparer : IComparer&lt;string&gt; {
      public int Compare(string x, string y) {
          double xVal, yVal;
  
          if(double.TryParse(x, out xVal) &amp;&amp; double.TryParse(y, out yVal))
              return xVal.CompareTo(yVal);
          else 
              return string.Compare(x, y);
      }
  }
  
  //Tested on int vs int, double vs double, int vs double, string vs int, string vs doubl, string vs string.
  //Not gonna put those here
  [TestMethod]
  public void RealWorldTest()
  {
      List&lt;string&gt; input = new List&lt;string&gt;() { "a", "1", "2,0", "b", "10" };
      List&lt;string&gt; expected = new List&lt;string&gt;() { "1", "2,0", "10", "a", "b" };
      input.Sort(new MixedNumbersAndStringsComparer());
      CollectionAssert.AreEquivalent(expected, input);
  }
  </code></pre>