title: How to use comparator in Java when sorting instances in a list of interface types?
body: <p>I have an interface (say Employee) which has 2 classes that implements it (say Assistant and Manager). I also have an interface which has a method that returns a list of the first interface type (say EmployeeLogic interface which returns List. There are two classes implementing this interface (say AssistantLogic and ManagerLogic). In these two classes, the List contains only Assistants (in the AssitantLogic) and only Manager (in the ManagerLogic). </p>
  
  <p>The problem is that I want to sort these two lists based on the methods that exist only in the subclasses. How do I write such a comparator?</p>
  
  <p>Probably the code is better understandable:</p>
  
  <pre><code>public interface Employee {
      public int employeeMethod();
  }
  
  public class Manager implements Employee {
      public int employeeMehod(){//[...]}
      public int managerMethod(){//[...]}
  }
  
  public class Assistant implements Employee {
      public int employeeMehod(){//[...]}
      public String assistantMehod(){//[...]}
  }
  
  public interface EmployeeLogic() {
      public List&lt;Employee&gt; getEmployees();
  }
  
  public class ManagerLogic implements EmployeeLogic {
      public List&lt;Employee&gt; getEmployees() {
  
          List&lt;Employee&gt; employees = [...] //or List&lt;? extends Employee&gt; ?
          [...]
          Collections.sort(employees, new Comparator&lt;Manager&gt;() {
              @Override
              public int compare(Manager o1, Manager o2) {
                  return o1.managerMethod().compareTo(o2.managerMethod());
              }
          });
          return employees;
      }
  
  }
  
  public class AssistantLogic implements EmployeeLogic {
      public List&lt;Employee&gt; getEmployees() {
  
          List&lt;Employee&gt; employees = [...] //or List&lt;? extends Employee&gt; ?
          [...]
          Collections.sort(employees, new Comparator&lt;Assistant&gt;() {
              @Override
              public int compare(Assistant o1, Assistant o2) {
                  return o1.assistantMehod().compareTo(o2.assistantMehod());
              }
          });
      return employees;
      }
  }
  </code></pre>
  
  <p>Is there a way to write such a comparator?</p>
  
  <p>I know a workaround (see bellow) but is the above thing possible with only one list?</p>
  
  <pre><code>public class ManagerLogic implements EmployeeLogic {
          public List&lt;Employee&gt; getEmployees() {
  
              List&lt;Manager&gt; managers = new ArrayList&lt;Manager&gt;()
              [...]
              Collections.sort(managers, new Comparator&lt;Manager&gt;() {
                  @Override
                  public int compare(Manager o1, Manager o2) {
                      return o1.managerMethod().compareTo(o2.managerMethod());
                  }
              });
              List&lt;Employee&gt; employees = new ArrayList&lt;Employee&gt;();
              employees.addAll(managers);
              return employees;
          }
  
      }
  </code></pre>