Please what am i doing wrong in this code
<p>I wrote a sorting function it works sorts everything, but if there is a space between two words it sorts the space to the beginning and removes the last string element  why is this happening? i tried String.trim() method to get rid of white spaces but it didn't work, i need help</p>

<pre><code>private void SortStringsActionPerformed(java.awt.event.ActionEvent evt) {                                            
         // TODO add your handling code here:
         String str = TextArea.getText();

     String[] words = str.split(" ");

        int length = words.length;
    String temp; 
    for(int i=0; i&lt;=length-1; i++){
            for(int j=0; j &lt;length-1;j++){
            if(words[i].compareToIgnoreCase(words[j]) == 0 ){}
            else if (words[i].compareToIgnoreCase(words[j]) &lt; 0){   
                                temp = words[i].trim();
                words[i] = words[j].trim();
                words[j] = temp;
            }else{} 
        }
    }
        /*
    String str2="";
    for (int i=0; i &lt; length-1; i++) {
            str2+=words[i]+" ";
        }
        */
        StringBuilder str2 = new StringBuilder();
        for(int i=0; i&lt;length-1; i++) {
            str2.append(words[i]).append(" ");
        }
        TextArea.setText(str2.toString());

    }                            
</code></pre>
