0

I need help with sorting an array that contains two different kind of classes that I have created. My two classes are "Human" that have a name and an age, then I have a class "Physicist" that inherits from human but also have the field "start year" when they started studying. Something like this:

public class Human implements Comparable<Human> {

   private int age;
   private String name;

   public Human(int agein, String namein) {  
      age = agein;
      name = namein;   
   }
   public int compareTo(Human other) {

      return Integer.compare(this.age, other.age);       
   }
}

public class Fysiker extends Human {

   private int year;

   public Fysiker(int startYear, int ageIn, String nameIn){

       super(ageIn, nameIn);
       }

   public int compareTo(Fysiker other) {

       if(other instanceof Fysiker && this.getAge() == other.getAge() ) {
           return Integer.compare(this.year, other.year);
       }
       else {
           return Integer.compare(this.getAge(), other.getAge());
       }

   }
}

What I want is that when I create an array mixed with humans and physicists and sort it, I want it to be sorted by age, and if two physicists are the same age, then they should get sorted by the year they have. For example like this:

Input:

name: Alex, age: 32, year: 2007

name: Nils, age: 30, year: 2008

name: Anders, age: 32, year: 2003

name: Erik, age: 18.

name: Olof, age: 31.

Sorted array:

name: Erik, age: 18.

name: Nils, age: 30, year: 2008

name: Olof, age: 31.

name: Anders, age: 32, year: 2003

name: Alex, age: 32, year: 2007

Are my compareTo methods wrong? Or why is it not working? I'm not getting any errors, the array just get sorted by age and then nothing more happens.

I'm thankful for your help!

5
  • 3
    add the language keyword please, makes it easier. Java? Commented Feb 29, 2016 at 12:18
  • Is it Java? If it is, add corresponding tag, please Commented Feb 29, 2016 at 12:26
  • Yes, it is Java. Sorry i forgot about that. Commented Feb 29, 2016 at 13:17
  • 1
    I think it should be "public int compareTo(Human other)", else only the base compareTo from Human is called (because the list you are sorting is probably a list of Human). Commented Feb 29, 2016 at 13:19
  • Do you have to implement Comparable, or could you also use an external Comparator? Commented Feb 29, 2016 at 13:23

2 Answers 2

1

This method:

public int compareTo(Fysiker other) {

    if(other instanceof Fysiker && this.getAge() == other.getAge() ) {
        return Integer.compare(this.year, other.year);
    }
    else {
        return Integer.compare(this.getAge(), other.getAge());
    }

}

will never be called, because you have an array of Human so the signature doesn't match (as mentioned by Arsen in the comments).

This should work:

public int compareTo(Human other) {

   if(other instanceof Fysiker && this.getAge() == other.getAge() ) {
       return Integer.compare(this.year, ((Fysiker) other).year);
   }
   else {
       return Integer.compare(this.getAge(), other.getAge());
   }

}
Sign up to request clarification or add additional context in comments.

Comments

0

Alternatively to implementing Comparable, you could also use a custom Comparator when sorting. Particularly with Java 8, this is also much simpler for comparing multiple fields, by chaining Comparator.comparing and Comparator.thenComparing with custom lambda functions.

In any way, the Comparator (or Comparable) has to accept any kind of Human and check whether it's a Fysiker or not.

List<Human> humans = Arrays.asList(
        new Fysiker(2007, 32, "Alex"),
        new Fysiker(2008, 30, "Nils"),
        new Fysiker(2003, 32, "Anders"),
        new Human(18, "Erik"),
        new Human(31, "Olof")
);
Collections.sort(humans, Comparator
        .comparing((Human h) -> h.age)
        .thenComparing((Human h) -> h instanceof Fysiker ? ((Fysiker) h).year : 0));
humans.forEach(System.out::println);

Output:

Human Erik 18
Fysiker Nils 30 2008
Human Olof 31
Fysiker Anders 32 2003
Fysiker Alex 32 2007

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.