title: How to compare generic nodes in a linked list using Comparable?
body: <p>I am implementing a sorted list using linked lists. My node class looks like this</p>
  
  <pre><code>public class Node&lt;E&gt;{
      E elem;
      Node&lt;E&gt; next, previous;
  }
  </code></pre>
  
  <p>In the sorted list class I have the add method, where I need to compare generic objects based on their implementation of compareTo() methods, but I get this syntax error
  "The method compareTo(E) is undefined for type E". I have tried implemnting the compareTo method in Node, but then I can't call any of object's methods, because E is generic type. 
  Here is the non-finished body of add(E elem) method.</p>
  
  <pre><code>public void add(E elem) 
  {
  
          Node&lt;E&gt; temp = new Node&lt;E&gt;();
          temp.elem = elem;
  
          if( isEmpty() ) {           
              temp.next = head;
              head.previous = temp;
              head = temp;
              counter++; 
          }else{
              for(Node&lt;E&gt; cur = head; cur.next != null ; cur= cur.next) {
                  **if(temp.elem.comparTo(cur.elem)) {**
                      //do the sort;
  
                  }/*else{
                      cur.previous = temp;
                  }*/             
              }
              //else insert at the end
  
          }
  }
  </code></pre>
  
  <p>Here is one of the object implemnting compareTo method</p>
  
  <pre><code>public class Patient implements Comparable&lt;Patient&gt;{
      public int compareTo(Patient that)
      {
          return (this.getPriority() &lt;= that.getPriority() ? 1 : 0 );
      }
  }
  </code></pre>