title: compareTo with generics for heapSort
body: <p>For class I had to either implement a BST or a heapSort. I did the BST but figured it would be good to know this too but now I'm stuck. This is my first time working with heaps(and really coding with generics/implementing Comparable so I apologize for all the errors) and im running into an issue implementing compareTo. </p>
  
  <p>Essentially I want to be able to add generic objects to my heap Array and then compare them for the Heap sorting. I use compareTo to check a new entry when adding to the heap and for swapping in the reheap method. </p>
  
  <p>My errors returned:</p>
  
  <pre><code>Heap.java:64: error: bad operand types for binary operator '&lt;'
  if (this  &lt; other)
            ^
  first type:  Heap&lt;T&gt;
  second type: Heap&lt;T&gt;
  
  where T is a type-variable:
  T extends Comparable&lt;T&gt; declared in class Heap
  </code></pre>
  
  <p>Im not sure how to work around that though. I understand that my binary operator isnt for generics but I dont know how to work around it. 
  Thanks for any input. Sorry about all the beginners mistakes you may find!
  Heres my code:</p>
  
  <pre><code>import java.util.*;
  
  class Heap&lt;T extends Comparable &lt;T&gt;&gt;  implements Comparable&lt;Heap&lt;T&gt;&gt;{
  
  private T[] heap;
  private int lastIndex;
  private static final int CAPACITY = 25;
  
  public Heap(){
  
      this(CAPACITY);
  
  }
  
  public Heap(int capacity){
  
      heap = (T[])new Comparable[capacity+1];
      lastIndex = 0;
  }
  
  public void add(T newEntry){
  
      lastIndex++;
      if(lastIndex&gt;=heap.length)
      doubleArray();
  
      int newIndex = lastIndex;
      int parentIndex = newIndex/2;
  
      while((parentIndex&gt;0)&amp;&amp;(heap[parentIndex].compareTo(newEntry)&gt;0))
      {
          heap[newIndex] = heap[parentIndex];
          newIndex = parentIndex;
          parentIndex = newIndex/2;
      }  
      heap[newIndex] = newEntry;
  }
  
  public void display()
  {
      for(int i=1;i&lt;heap.length;i++)
      {
          System.out.println(heap[i]);
      }
  
  }
  
  private void doubleArray()
  {
  T[] oldHeap = heap;
  int oldSize = heap.length;
  
  heap = (T[]) new Object[2*oldSize];
  
      for(int i =0; i &lt; oldSize-1;i++)
      {
          heap[i] = oldHeap[i];
      }
  }
  
  public int compareTo(Heap&lt;T&gt; other)
  {
      int sort = 0;
      if (this  &lt; other)
      {
              sort = -1;
      }
      else if (this&gt; other)
      {
              sort = 1;
      }
      else
      {
              sort = 0;
      }
      return sort;
  }
  
  private &lt;T extends Comparable&lt;T&gt;&gt; void reheap(T[] heap, int rootIndex, int lastIndex)
  {
      boolean done=false;
      T orphan = heap[rootIndex];
      int leftChildIndex = 2 * rootIndex + 1;
  
      while(!done &amp;&amp; (leftChildIndex&lt;=lastIndex))
      {
      int largerChildIndex = leftChildIndex;
      int rightChildIndex = leftChildIndex + 1;
  
          if(rightChildIndex&lt;=lastIndex &amp;&amp;     (heap[rightChildIndex].compareTo(heap[largerChildIndex])&gt;0))
              largerChildIndex = rightChildIndex;
          if(orphan.compareTo(heap[largerChildIndex])&lt;0)
          {
          //  System.out.println(orphan+ "--" + largerChildIndex);
  
              heap[rootIndex] = heap[largerChildIndex];
              rootIndex = largerChildIndex;
              leftChildIndex = 2 * rootIndex+1;
          }
          else
              done = true;
      }
  heap[rootIndex] = orphan;
  }
  
  public &lt;T extends Comparable&lt;T&gt;&gt; void heapSort(int n)
  {
      for(int rootIndex = n/2-1;rootIndex &gt;=0;rootIndex--)
          reheap(heap,rootIndex,n-1);
  
      swap(heap,0,n-1);
  
      for(int lastIndex = n-2;lastIndex &gt; 0;lastIndex--)
      {   
          reheap(heap,0,lastIndex);
          swap(heap,0,lastIndex);
      }
  }
  
  private &lt;T extends Comparable&lt;T&gt;&gt; void swap(T[] a,int first, int last)
  {
  T temp;
  
  temp = a[first];
  a[first] = a[last];
  a[last] = temp;
  }
  
  }
  </code></pre>
  
  <p>Any help with any of this is very very appreciated</p>