Gustavo Amgarten
Gustavo Amgarten

Reputation: 406

Sort Superclass list of objects that are Subclasses of the Superclass

Suppose I have an abstract Superclass named that is defined like this:

public abstract class Member implements Comparable<Member>

And then I have two Subclasses of Member, defined like this:

public class Coach extends Member

public class Player extends Member

Then, I have a list of Member, which means it contains both Coach and Player.

I need to sort this list, in which a coach always comes first and then come the players sorted by an identification number.

I need to implement the compareTo method to do this. But how do I proceed, knowing that I can't implement the method in the superclass Member, since the identification number is a property of Player and Coach doesn't have it?

I hope I've been clear on the problem.

Upvotes: 2

Views: 1436

Answers (3)

Avneet Singh
Avneet Singh

Reputation: 1

import java.util.*;

abstract class VIP implements Comparable<Member>{
 abstract  int getOrderIndex();
 abstract String getName();
}


class Member extends VIP {
    String name;
    int index;
    public Member(String s,int i){
    this.name=s;this.index=i;
    }
    
    public  int compareTo(Member m){
       return this.getOrderIndex()-m.getOrderIndex();
     }
    
    public String getName(){
    return name;
    }
    public int getOrderIndex(){
    return index;
    }
}

class Player extends Member {
      public Player(String s,int i){
      super(s,i);
      } 
}
class Coach extends Member{
    public Coach(String s,int i){
      super(s,i);
    }
}
class Refree extends Member{
    public Refree(String s,int i){
      super(s,i);
    }
}

class Main{
  public static void main(String[] args) {
        
        List<Member> l=new ArrayList<Member>();
        
         l.add(new Refree("refree",2));
         l.add(new Player("player2",1));
         l.add(new Player("player1",1));
         l.add(new Coach("coach2",0));       
         l.add(new Coach("coach1",0));      
       
        l.sort((m1,m2)-> m1.compareTo(m2));
       
        l.forEach(o-> System.out.println(o.getName()));
       
       
  }
}

Upvotes: 0

Norbert Radyk
Norbert Radyk

Reputation: 2618

You've got a couple of options here:

  • Write a compareTo method on Member and write a compator which will check whether member object is the instance of Coach (then it will be always before other Member object).

In this solution, however you assume however that base class will know about it's implementors which might be considered a bad design, as once you start introducing more implementing classes (i.e. Referee, Technical Assistant etc.) you'll need to change the method every time, and might end up with something really complicated.

On the other hand though you've got the full ordering control in one place, which is easier to test and reason about than...

  • Alternatively extend member with getOrderingIndicator() abtract method which you'll implement in both Player (assign identification number) and Coach (assign 0 or some other value lower than allowed player identification number) and use this abstract method in the compareTo method in Member class.

In this solution you'll guarantee that whatever implementations of Member base class there are in the future, they will be correctly ordered.

All in all though, I'd strongly suggest implementing the compareTo on the member level, since it's the Member objects you're comparing.

Upvotes: 4

Jigar Joshi
Jigar Joshi

Reputation: 240928

You need to implement Comparator and check object's class and take decision

abstract class Member implements Comparator<Base>{
    @Override
    public int compare(Base o1, Base o2) {
        if(o1.getClass() == Couch.class && o2.getClass() == Player.class){
            return Integer.MAX_VALUE;
        }

        if(o1.getClass() == Player && o2.getClass() == Coach.class){
            return Integer.MIN_VALUE;
        }

        if(o1.getClass()==o2.getClass() && o1.getClass() == Coach.class){
            return 0;
        }

        if(o1.getClass() == o2.getClass() && o2.getClass() == Player.class){
            //cast to player and compare them
        }

    }
}

Upvotes: 2

Related Questions