title: How does the Comparable interface work?
body: <p>I am learning Java and want to sort an array containing objects. To do that I want to use Comparable. I consistently run into a compile error that seems to point out that I either did not implement compareTo (I did) or have something wrong with Comparator (which I do not want to use by now). I have isolated the problem in a small program that I list below. Any help is greatly appreciated because I have spent hours searching on the web.</p>
  
  <p>I have commented out some code of main because what I really want to do is to create an array with interface objects and sort it using Comparable. I am not sure whether that is possible, so I am now testing sorting classes.</p>
  
  <p>package comp_test;</p>
  
  <pre><code>  import java.util.ArrayList;
    import java.util.List;
  
    public class program {
  
      public static void main(String[] args) 
      {
          List&lt;root&gt; container = new ArrayList&lt;root&gt; ();
  
          container.add (new A ("Een"));
    //        container.add (new AA ("twee"));
          container.add (new B ("drie"));
    //        container.add (new AA ("vier"));
          container.add (new A ("en Vijf"));
  
          for (root r: container)
          {
              r.show ();
          } // for
          List.sort (container); // &lt;=== error
          // The method sort(Comparator) in the type List is not 
          // applicable for the arguments (List&lt;root&gt;
      }
  
    /*
      public static void main(String[] args) 
  
      {
          List&lt;Itf&gt; container = new ArrayList&lt;Itf&gt; ();
  
          container.add (new A ("Een"));
          container.add (new AA ("twee"));
          container.add (new B ("drie"));
          container.add (new AA ("vier"));
          container.add (new A ("en Vijf"));
  
          for (Itf i: container)
          {
              i.show ();
          } // for
          container.sort ();
      }
    */
    }
  
    --------------
    package comp_test;
  
    public interface Itf 
    {
      public void show ();
    }
    ---------------
    package comp_test;
  
    abstract public class root  implements Itf, Comparable&lt;root&gt; 
    {
      abstract public void show ();
      abstract public String getID ();
  
      @Override
      public int compareTo (root other) 
      {
          return this.getID ().compareTo (other.getID ());
      } /*** compareTo ***/
  
    }
    ------------------
    package comp_test;
  
    public class A extends root  
    {
      private String ID;
  
      public A (String id) 
      {
          this.ID = id;
      } /*** A ***/
  
      public String getID ()
      {
          return ID;
      } /*** getID ***/
  
      @Override
      public void show ()
      {
          System.out.println ("A (" + getID () + "): " + super.toString ());
      } /*** show ***/
  
    }
    ------------------
    package comp_test;
  
    public class B extends root 
    {
      private String Name;
  
      public B (String id)
      {
          this.Name = id;
      } /*** B ***/
  
      @Override 
      public String getID ()
      {
          return Name;
      } /*** getID ***/
  
      @Override
      public void show ()
      {
          System.out.println ("B (" + Name + "): " + super.toString ());
      } /*** show ***/
  
    }
  </code></pre>