title: Order alphanumeric string numerically, and then by prefix/suffix
body: <p>I have a complicated sorting pattern to replicate, and my solution seems a bit hamfisted.
  My input is a list of numbers that can have several letters as a suffix and prefix both are only letter ('aaa', 'aab', 'ac' etc). 
  I need to sort numerically, then sort by suffix (if there is one) and then by prefix (if there is one).</p>
  
  <p>E.g. </p>
  
  <pre><code>"a1a",
  "5ac",
  "1",
  "12",
  "2",
  "11",
  "5aa",
  "3",
  "5ab",
  "a2b",
  "abb11ca",
  "1b",
  "aba11ca"
  </code></pre>
  
  <p>would be sorted as</p>
  
  <pre><code>1
  a1a
  1b
  2
  a2b
  3
  5aa
  5ab
  5ac
  11
  aba11ca
  abb11ca
  12
  </code></pre>
  
  <p>Here is the solution that I came up with using Linq.</p>
  
  <pre><code>static void Main(string[] args)
  {
      var arr = new []
      {"b2","a1a","5ac","1","12","2","11","5aa","3","5ab","a1","a2b","abb11ca","1b","aba11ca"
      };
      var ordered = arr.Select(str =&gt; {
                  var parts = SplitIntoPrefixNumberSuffix(str);
                  var number = int.Parse(parts[1]);
                  return new { str, parts, number };
              })
              .OrderBy(x =&gt; x.number).ThenBy(x =&gt; x.parts[2]).ThenBy(x =&gt; x.parts[0])
              .Select(x =&gt; x.str);
  
      Console.WriteLine("sorted array: ");
      foreach (var s in ordered)
      {
          Console.WriteLine("{0}", s);
      }
  
      Console.ReadLine();
  }      
  
  public static string[] SplitIntoPrefixNumberSuffix(string str)
  {
      var numChar = new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
      var numLoc = str.IndexOfAny(numChar);
      var nums = "";
      foreach (var c in str)
      {
          if (char.IsDigit(c))
              nums = nums + c;
      }
      Console.WriteLine("numLoc: {0}; nums: {1}", numLoc, nums.Count());
      var prefix = str.Substring(0, numLoc);
      var suffix = str.Substring(numLoc + nums.Count());
      Console.WriteLine("prefix {0}; nums {1}; suffix {2}", prefix, nums, suffix);
      return new[] { prefix, nums, suffix };
  }
  </code></pre>
  
  <p>Here is a .netfiddle of it working: <a href="https://dotnetfiddle.net/C7ZA0b" rel="nofollow noreferrer">https://dotnetfiddle.net/C7ZA0b</a>.</p>
  
  <p>While it works, it feels like it's not a very good solution. I'm iterating over the collection several times, and I think I should be using a custom comparable.</p>
  
  <p>I've never written a Comparable before; I looked at <a href="https://www.dotnetperls.com/alphanumeric-sorting" rel="nofollow noreferrer">Dot Net Pearls Alphanumeric Sorting</a> and can follow it but not well enough to modify it to suit my needs.</p>
  
  <p>Is there an IComparable I can use to do the above job? Any suggestions on a good place to learn how to write one?</p>