title: Sorting a Linked List of Strings
body: <pre><code>public class LinkedList {
      private Node top;
  
      public LinkedList() {
          top = null;
      }
  
      public void add(String data) {
          Node temp = new Node(data, top);
          top = temp;
      }
  
      public void sort() {
          LinkedList sortedList = new LinkedList();
          Node i;
          Node j;
          String temp;
          String temp2;
          String temp3;
          for (i = top; i != null; i.getNext()) {
              for (j = i.getNext(); j != null; j.getNext()) {
                  if (i.getData().compareTo(j.getData()) &lt; 0) {
                      temp = i.getData();
                      i.setData(temp);
                      temp2 = j.getData();
                      j.setData(temp2);
                      temp3 = temp;
                      temp = temp2;
                      temp2 = temp3;
                      sortedList.add(temp3);
                  } else if (i.getData().compareTo(j.getData()) &gt; 0) {
                      temp3 = i.getData();
                      sortedList.add(temp3);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Can someone go over my code and tell me why my <code>temp</code> and <code>temp2</code> never get assigned and used? And why this isn't working?  </p>
  
  <p>When I run my <code>main</code> I just get the original linked list not the sorted one.</p>
  
  <p><strong>Is my logic correct</strong>? I am trying to sort the strings in an ascending order.</p>