title: Convert list to number range string
body: <p>This question is pretty much the opposite of this question:
  <a href="https://stackoverflow.com/questions/40161/does-c-have-built-in-support-for-parsing-page-number-strings">Does C# have built-in support for parsing page-number strings?</a></p>
  
  <p>So given </p>
  
  <pre><code>1,3,5,6,7,8,9,10,12:
  </code></pre>
  
  <p>I will ouput:</p>
  
  <pre><code>1,3,5-10,12
  </code></pre>
  
  <p>Here is my first attempt.  It seems kind of hacky and is probably the worst code I ever wrote.  Can you suggest an imporovement\better way to do it?</p>
  
  <pre><code>static string numListToRangeStr(List&lt;int&gt; numList)
  {
      StringBuilder retString = new StringBuilder();
      numList.Sort();
  
      bool inRangeFind = false;
      int firstInRange = numList[0];
      int lastNumber = firstInRange;
      bool first = true;
  
      for (int i = 1; i &lt; numList.Count; i++)
      {
          if (numList[i] == (lastNumber + 1))
          {
              inRangeFind = true;
          }
          else
          {             
              if (inRangeFind)
              {
                  if (!first)
                  {
                      retString.Append(",");
                  }
                  retString.Append(firstInRange);
                  retString.Append("-");
              }
              else
              {
                 if (!first)
                  {
                      retString.Append(",");
                  }
              }
  
              retString.Append(lastNumber);
  
              firstInRange = numList[i];
              inRangeFind = false;
              first = false;
          }
  
          lastNumber = numList[i];
      }
  
  
      if (inRangeFind)
      {
          if (!first)
          {
              retString.Append(",");
          }
          retString.Append(firstInRange);
          retString.Append("-");
      }
      else
      {
          if (!first)
          {
              retString.Append(",");
          }
      }
      retString.Append(lastNumber);
  
      return retString.ToString();
  }
  </code></pre>