title: Using collections.sort() with compareTo, not sorting
body: <p>So currently I'm working on an assignment where I have to two classes, one is named Fysiker and the other Human. Fysiker is simply an extension of the Human class. Human has two attributes: name and age while Fysiker has three: name, age and startyear. I have created an array which takes both Human and Fysiker, and I want to sort it by age first, but if two fysiker have the same age, I want it to sort by my third attribute: the startyear. </p>
  
  <p>My understanding of the compareTo (I need to use compareTo, for the sake of the exercise) is limited, but from what I've read it's a comparable which interacts automatically with the Collections.sort(), my code currently looks like this:</p>
  
  <pre><code>import java.util.List;
  import java.util.Arrays;
  import java.util.Random;
  import java.util.ArrayList;
  import java.util.Collections;
  public class Fysiker extends Human{
  
      public int startyear;
      public Fysiker(int age, String name, int startyear){
          this.age=age;
          this.name=name;
          this.startyear=startyear;
      }
  
      public int getYear(){
          return startyear;
      }
      public int compareTo(Fysiker o){
          int b;
          b=(this.age&gt;o.age ? 1:this.age&lt;o.age ? -1:0);
          if (b==0){
              b=(this.startyear&gt;o.startyear ? 1:this.startyear&lt;o.startyear ? -1:0);
              return b;}
              else{
              return b;}
  
      }
      public String toString(){
          return "åldern är:"+this.age+" "+"namnet är:"+this.name+" "+"började fysik:"+String.format("F%02d",this.startyear%100);
      }
  
      public Fysiker(){
          this.age=15+rand.nextInt(86);
          this.name=names.get(rand.nextInt(names.size()));
          this.startyear=2015-rand.nextInt(this.age-14);
          while (this.startyear&lt;1932){
              this.startyear=2015-rand.nextInt(this.age-14);
      }
  }
      public static void main(String[] args){
          ArrayList&lt;Human&gt; fysiker=new ArrayList&lt;Human&gt;();
          int q;
  
  
          for (q=0;q&lt;=80;q++){
              fysiker.add(new Fysiker());
              //fysiker.add(new Human());
  
              //System.out.println(fysiker.get(q).toString());
          }
          int s;
          Collections.sort(fysiker);
          for (s=0;s&lt;=fysiker.size()-1;s++){
              //fysiker.get(0).compareTo(fysiker.get(s));
              System.out.println(fysiker.get(s).toString());
              //System.out.println(fysiker.get(0));
          }
      }
  
  }
  </code></pre>
  
  <p>the Human class in turn:</p>
  
  <pre><code>import java.util.List;
  import java.util.Arrays;
  import java.util.Random;
  import java.util.ArrayList;
  public class Human implements Comparable&lt;Human&gt;{
          public static final List&lt;String&gt; names = Arrays.asList("Rutger","Oscar","Aram","Noak","Hilda","Dahl");
          public Random rand=new Random();
          public String name;
          public int age;
  
      public Human(int age, String name){
          this.age=age;
          this.name=name;
  
      }
      public Human(){
          this.age=rand.nextInt(101);
          this.name=names.get(rand.nextInt(names.size()));
      }
  
      public String getName(){
          return this.name;
      }
      public int getAge(){
          return this.age;
      }
      public String toString(){
          return "åldern är:"+this.age+" "+"namnet är:"+this.name;
      }
      public int compareTo(Human o){
          return this.age-o.age;
  }
  }
  </code></pre>
  
  <p>My issue is that it does not sort after the startyear, but I can fix it if I remove compareTo from my Human class, but I need that one as well, I want to sort them all, but it fixes the "not sorting after startyear for the fysiker"-problem. Where do I go from here ?</p>