title: Why get class cannot be cast to class java.lang.Comparable
body: <p>I'm iterating all <code>Student</code> data using natural sorting method of java 8 <code>sorted()</code>. While iterating student data, get exception in IDE console <code>class com.java8.Student cannot be cast to class java.lang.Comparable</code>. My <code>StreamStudent.java</code> file is inside <code>com.java8</code> package.</p>
  <p>Here is my full stack trace:</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.ClassCastException: class com.java8.Student cannot be cast to class java.lang.Comparable (com.java8.Student is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')
      at java.base/java.util.Comparators$NaturalOrderComparator.compare(Comparators.java:47)
      at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
      at java.base/java.util.TimSort.sort(TimSort.java:220)
      at java.base/java.util.Arrays.sort(Arrays.java:1307)
      at java.base/java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:353)
      at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:510)
      at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
      at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
      at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
      at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
      at com.java8.StreamStudent.main(StreamStudent.java:72)
  </code></pre>
  <p>Here down is my code:</p>
  <pre><code>package com.java8;
  
  import java.util.*;
  import java.util.stream.*;
  
  class Student{
      String firstName;
      String lastName;
      Integer groupId;
      Integer age;
  
      // constructor and getter, setter
  
  }
  
  public class StreamStudent {
      public static void main(String[] args) {
          List&lt;Student&gt; stdList = new ArrayList&lt;&gt;();
          stdList.add(new Student(&quot;Sara&quot;, &quot;Mills&quot;, 1, 18));
          stdList.add(new Student(&quot;Andrew&quot;, &quot;Gibson&quot;, 2, 21));
          stdList.add(new Student(&quot;Craig&quot;, &quot;Ellis&quot;, 1, 23));
          stdList.add(new Student(&quot;Steven&quot;, &quot;Cole&quot;, 2, 19));
          stdList.add(new Student(&quot;Andrew&quot;, &quot;Carter&quot;, 2, 2));
  
          
          System.out.println(&quot;1. Sort all student firstname by name&quot;);
          List&lt;Student&gt; students =  stdList.stream().sorted().collect(Collectors.toList());
          students.forEach((s) -&gt; System.out.println(s.getFirstName() + &quot; &quot; + s.getLastName() + &quot; &quot; + s.getGroupId() + &quot; &quot; + s.getAge()));
      }
  }
  </code></pre>