title: Using Bubble Sort to Alphabetically Sort Array of Names in Java
body: <p>I've been trying to tackle this bug for a while, but I can't get around to it. The purpose of the program below is to use bubble sort to alphabetically order an array of names. For example, if the names are ["Bob Joe", "Bob Frank", and "Bob Johnson"], the correctly sorted array would be ["Bob Frank", "Bob Joe", "Bob Johnson"].</p>
  
  <p>The main challenge I am having is comparing any 2 strings past name.charAt(0). If I only compare the characters of any 2 strings at 1 specific index point, my code works. However, if I try to make the comparison move past index 0 if index 0 of both strings are equal to each other, my program no longer works.</p>
  
  <p>The code is outlined below</p>
  
  <pre><code>public static void sortAlpha (String names[])
      {
          for (int i = 0 ; i &lt; names.length - 1 ; i++)
          {
              for (int a = 0 ; a &lt; names.length - 1 - i ; a++)
              {
  
                  int length1 = names [a].length ();
                  int length2 = names [a + 1].length ();
  
                  int min = 1; 
  
                  if (length1 &gt; length2)
                  {
                      min = length2;
                  }
                  else
                  {
                      min = length1;
                  }
  
                  for (int b = 0 ; b &lt; min ; b++)
                  {
  
                      if ((int) names [a].toLowerCase ().charAt (b) &gt; (int) names [a + 1].toLowerCase ().charAt (b))
                      {
                          String tempName = names [a];
  
                          // sort:
                          names [a] = names [a + 1];
                          names [a + 1] = tempName;
  
                          break;
                      }
  
                  }
  
              }
          }
      }
  </code></pre>
  
  <p>If I simply default the min value to 1, the code runs and does its intended job. However, if the min value stays dynamic, the program does not work. I'm trying to discern why this is so and what the fix is. Any help would be appreciated!</p>