title: Java- Manually sort a string array using compareTo() method
body: <p>Let me first say that I know that there a better ways to sort where you might use something other than an array. This an assignment for class where the user can store strings in an array, delete, display, and sort them. I am completely lost to where to go from here. I am trying to use a bubble sort and everything works except whatever is the first entry in my array won't sort. I avoid null pointer exceptions through filtering out null values which is why my if statement is so long. </p>
  
  <pre><code>private void sortItems(String[] cargohold) {
      String temp;
      for (int i = 0; i &lt; cargohold.length - 1; i++) {
          for (int j = 1; j &lt; cargohold.length; j++) {
              if (cargohold[i] != null &amp;&amp; cargohold[j] != null &amp;&amp; (cargohold[i].compareTo(cargohold[j]) &lt; 0)) {
                  temp = cargohold[j];
                  cargohold[j] = cargohold[i];
                  cargohold[i] = temp;
              }   
          }
      }
  }
  </code></pre>
  
  <p>I have tried a bunch of different ways of doing this and I can't find any good reason why this shouldn't work. I have looked through anything I could find on Stack Overflow as far as examples and no one is having the same issue I am. </p>
  
  <p>To recap, I may have 5 strings, "Derp", "Herp", "Sam", "Alfred", "Bill" and this sort will give me: "Derp", "Alfred", "Bill", "Herp", "Sam". Thanks in advance for the guidance. </p>