title: MyClass cannot be cast to java.lang.Comparable: java.lang.ClassCastException
body: <p>I am doing a java project and I got this problem and don't know how to fix it.</p>
  
  <p>The classes in my project (simplified): </p>
  
  <pre><code>public class Item {
  
      private String itemID;
      private Integer price;
  
      public Integer getPrice() {
  
          return this.price;
  
      }
  
  }
  
  public class Store {
  
      private String storeID;
      private String address;
  
  }
  
  public class Stock {
  
      private Item item;
      private Store store;
      private Integer itemCount;
  
      public Integer getInventoryValue() {
  
          return this.item.getPrice() * this.itemCount;
  
      }  
  }
  </code></pre>
  
  <p>Then I try to sort an <code>ArrayList</code> of Stock so I create another class called <code>CompareByValue</code></p>
  
  <pre><code>public class CompareByValue implements Comparator&lt;Stock&gt; {
  
      @Override
      public int compare(Stock stock1, Stock stock2) {
  
          return (stock1.getInventoryValue() - stock2.getInventoryValue());
  
      }
  </code></pre>
  
  <p>}</p>
  
  <p>When I try to run the program, it gives the error:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.ClassCastException: Stock cannot be cast to java.lang.Comparable</p>
  </blockquote>
  
  <p>Anyone know what's wrong?</p>