title: Class cannot be cast to java.lang.Comparable
body: <p>First of all, I know some people has already asked similar questions, but I'm not sure if this is the same as what they asked. </p>
  
  <p>I have a class named <code>Vehicle</code> which has an attribute named <code>location</code>. On the other side, I have a class named <code>Road</code> which has an ArrayList attribute of Vehicles. I want to order the list, named <code>_vehicles</code>, using <code>location</code> but in descending order, so I created this class into the Road class:</p>
  
  <pre><code>class CompareVehicles implements Comparator&lt;Vehicle&gt; {
  
      @Override
      public int compare(Vehicle o1, Vehicle o2) {
          if (o1.getLocation() &lt; o2.getLocation()) return 1;
          if (o1.getLocation() &gt; o2.getLocation()) return -1;
          return 0;
      }
  }
  </code></pre>
  
  <p>And an attribute named <code>_vehicleComparator</code> which its type is <code>CompareVehicles</code>. But, when I execute the method <code>_vehicles.sort(_vehicleComparator)</code>, I get this error:</p>
  
  <pre><code>java.lang.ClassCastException: class simulator.model.Vehicle cannot be cast to class java.lang.Comparable (simulator.model.Vehicle is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')
      at java.base/java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:320)
      at java.base/java.util.ComparableTimSort.sort(ComparableTimSort.java:188)
      at java.base/java.util.Arrays.sort(Arrays.java:1316)
      at java.base/java.util.Arrays.sort(Arrays.java:1510)
      at java.base/java.util.ArrayList.sort(ArrayList.java:1717)
      at simulator.model.Road.advance(Road.java:115)
      at simulator.model.TrafficSimulator.advance(TrafficSimulator.java:44)
      at simulator.control.Controller.run(Controller.java:54)
      at simulator.launcher.Main.startBatchMode(Main.java:136)
      at simulator.launcher.Main.start(Main.java:144)
      at simulator.launcher.Main.main(Main.java:156)
  </code></pre>
  
  <p>Should <code>Vehicle</code> implement the <code>Comparable</code> interface, even if I'm using a Comparator? Or am I missing something?</p>
  
  <p>Thank you in advance </p>