title: C# merge sort string array
body: <p>I am playing around with sorting arrays and I figured out how to MergeSort an int array. But I cant figure out to MergeSort a string array. Sorting string array is easy enough when normal sorting but Merge Sort is different. The code I have done so far is below and is working on int arrays. </p>
  
  <pre><code>public int number = 1;
  public void mergeSort(int[] sortArray, int lower, int upper)
      {
          int middle;
          if (upper == lower)
              return;
          else
          {
              middle = (lower + upper) / 2;
              mergeSort(sortArray, lower, middle);
              mergeSort(sortArray, middle + 1, upper);
              Merge(sortArray, lower, middle + 1, upper);
          }
      }
  public void Merge(int[] sortArray, int lower, int middle, int upper)
      {
          string[] temp = new string[sortArray.Length];
          int lowEnd = middle - 1;
          int low = lower;
          int n = upper - lower + 1;
          while ((lower &lt;= lowEnd) &amp;&amp; (middle &lt;= upper))
          {
              if (sortArray[lower] &lt;= sortArray[middle])
              {
                  temp[low] = sortArray[lower].ToString();
                  low++;
                  lower++;
              }
              else
              {
                  temp[low] = sortArray[middle].ToString();
                  low++;
                  middle++;
              }
          }
          while (lower &lt;= lowEnd)
          {
              temp[low] = sortArray[lower].ToString();
              low++;
              lower++;
          }
          while (middle &lt;= upper)
          {
              temp[low] = sortArray[middle].ToString();
              low++;
              middle++;
          }
          for (int i = 0; i &lt; n; i++)
          {
              sortArray[upper] = Int32.Parse(temp[upper]);
              upper--;
          }
      }
  private void btnExecute_Click(object sender, EventArgs e)
      {
          String arraylength;
          int num;
          arraylength = Microsoft.VisualBasic.Interaction.InputBox("Enter a number to determine the length of the array", "Enter Number");
          try
          {
              while (!(int.TryParse(arraylength, out num)))
              {
                  MessageBox.Show("Not a valid number, try again.");
                  arraylength = Microsoft.VisualBasic.Interaction.InputBox("Enter a number a to determine the length of the array", "Enter Number");
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show("Value entered is not in a valid format");
          }
          int intlength = Int32.Parse(arraylength);
  
          string[] stringsArray = new string[intlength];
  
  
          int arraypos = 0;
          // For display purposes
          int positionvalue = 1;
          txtOutput.Text += "Unsorted array: \r\n";
          foreach (string s in stringsArray)
          {
              string arrayvalue = Microsoft.VisualBasic.Interaction.InputBox("Enter a number for array value" + positionvalue, "Enter Number");
              string arrayvalues = arrayvalue;
              stringsArray[arraypos] = arrayvalues.ToString();
  
              txtOutput.Text += arrayvalues + "\t";
              arraypos++;
              positionvalue++;
          }
          mergeSort(stringsArray, 0, stringsArray.Length - 1);
  
          txtOutput.Text += "\r\nSorted array: \r\n";
          foreach (string i in stringsArray)
          {
              txtOutput.Text += i + "\t";
          }
  
      }
  </code></pre>