title: Insertion Sort on an array of strings in C#
body: <p>If I have an array of strings, such as</p>
  
  <pre><code>string[] names = {"John Doe", "Doe John", "Another Name", "Name Another"};
  </code></pre>
  
  <p>How do I sort this array, using insertion sort?</p>
  
  <p>Wikipedia has some examples: <a href="https://en.wikibooks.org/wiki/Algorithm_implementation/Sorting/Insertion_sort#C.23" rel="nofollow noreferrer">https://en.wikibooks.org/wiki/Algorithm_implementation/Sorting/Insertion_sort#C.23</a></p>
  
  <pre><code>static void InsertSort(IComparable[] array)
  {
      int i, j;
  
      for (i = 1; i &lt; array.Length; i++)
      {
          IComparable value = array[i];
          j = i - 1;
          while ((j &gt;= 0) &amp;&amp; (array[j].CompareTo(value) &gt; 0))
          {
              array[j + 1] = array[j];
              j--;
          }
          array[j + 1] = value;
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>static void InsertSort&lt;T&gt;(IList&lt;T&gt; list) where T : IComparable&lt;T&gt;
  {
      int i, j;
  
      for (i = 1; i &lt; list.Count; i++)
      {
          T value = list[i];
          j = i - 1;
          while ((j &gt;= 0) &amp;&amp; (list[j].CompareTo(value) &gt; 0))
          {
              list[j + 1] = list[j];
              j--;
          }
          list[j + 1] = value;
      }
  }
  </code></pre>
  
  <p>but it doesn't seem to work on my array of strings, unless I'm doing something wrong.</p>
  
  <p>Would I not run</p>
  
  <pre><code>InsertSort(names); // like so?
  </code></pre>