title: in what way is a comparator superior to comparable?
body: <p>"How will you sort collection of employee objects by its id or name". For that we can use two interfaces, i.e., <code>Comparator and Comparable.</code> 
  seems this is one of the common interview questions</p>
  
  <p>But I don't see a reason why I should use both for sorting employee objects</p>
  
  <p>I have been thinking on what <code>comparator</code> accomplishes that <code>Comparable</code> cannot do.
  I understand that if the objects (instance variables that is compared upon) have natural ordering then <code>comparable</code> is the right choice.
  but if custom ordering is needed (eg string length) then one could write a <code>comparator.</code>
  my point here is <code>comparator</code> is only needed by the client if he wants to sort the data by some other criteria.
  For example, I would implement an <code>Employee class</code> to sort by <code>id</code> using <code>comparable interface</code>.
  but if the client wants to sort Employee objects by <code>String</code>(name), he would implement <code>comparator</code> either as a concrete class or anonymously in sorting.
  Is there anything I am missing here?</p>
  
  <p>For example, In the following code, for the Person object, my compareTo method, compares the age and sort it
  In the compare method, I use String length (name of the person) for sorting. In theory, I could accomplish both in the compareTo method as I have implemented below.</p>
  
  <p>lastly, are there any added benefits of one of the following over other
  I have implemented comparator in two ways
  1. as a static method which is commented out
  2. as anonymous object(?) in the main method which is commented out
  3. make a new class that implements comparator and call the instance of that class in collections.sort() -- this I have not done here</p>
  
  <pre><code>(The commented-out parts of the code works. They are just different implementations)
  
  mport java.util.Collections;
  import java.util.Comparator;
  import java.util.*;
  
  public class PersonComparator implements Comparable{
      private String name;
      private int age;
  
      public PersonComparator(String name, int age) {
          this.name = name;
          this.age = age;
      }
  @Override
  public String toString() {
      return "name=" + name + ", age=" + age;
  }
  
  /*@Override
  public int compareTo(Object obj) {
      if (!(obj instanceof PersonComparator)) {
          throw new ClassCastException("Invalid object");
      }
      PersonComparator p2 = (PersonComparator)obj;
      return this.age-p2.age;
  }*/
  
  /*Alternative CompareTo that checks for both age and name*/
   public int compareTo(Object obj) {
      if (!(obj instanceof PersonComparator)) {
          throw new ClassCastException("Invalid object");
      }
      PersonComparator p2 = (PersonComparator)obj;
      if (this.age!=p2.age){
          return this.age-p2.age;
      }
      else {
      return (this.name.length()-p2.name.length());
  }
  } 
  
  
  /*public static Comparator nameLengthComparator 
  = new Comparator() {
  
  
      @Override
      public int compare(Object obj1, Object obj2) {
          if (!(obj1 instanceof PersonComparator) || !(obj2 instanceof PersonComparator)){
              throw new ClassCastException("Invalid object");
          }
          else {
              PersonComparator p1 = (PersonComparator)obj1;
              PersonComparator p2 = (PersonComparator)obj2;
              return p1.name.length()-p2.name.length();
          }
  }
  };*/
  
   public static void main(String[] args){
       PersonComparator p1 = new PersonComparator("Alexander", 45);
       PersonComparator p2 = new PersonComparator("Pat", 27);
       PersonComparator p3 = new PersonComparator("Zacky", 45);
       PersonComparator p4 = new PersonComparator("Rake", 34);
  
       List&lt;PersonComparator&gt; list = new ArrayList&lt;PersonComparator&gt;();
       list.add(p1);
       list.add(p2);
       list.add(p3);
       list.add(p4);
  
       System.out.println("Before sorting "+ list);
       Collections.sort(list);
       //System.out.println("After sorting by age "+ list);
       //System.out.println("Before sorting "+ list);
       //Collections.sort(list, nameLengthComparator);
       System.out.println("After sorting by name length "+ list);
       /*Collections.sort(list, new Comparator&lt;PersonComparator&gt;() {
           @Override
              public int compare(PersonComparator p1, PersonComparator p2) {
                      return p1.name.length()-p2.name.length();
                  }
          }
      );*/
       System.out.println("After sorting by name length "+ list);
   }
  
  }
  </code></pre>
  
  <p>Thanks</p>