title: java comparator, how to sort by integer?
body: <p>Im trying to learn comparator in java and I have found this great example online, my question is how would this code be changed so that the pet names are ordered by age and in descending order so that the oldest is first and youngest is last? </p>
  
  <pre><code>class Dog implements Comparator&lt;Dog&gt;, Comparable&lt;Dog&gt;{
  private String name;
  private int age;
  Dog(){
  }
  
  Dog(String n, int a){
    name = n;
    age = a;
  }
  
  public String getDogName(){
    return name;
  }
  
  public int getDogAge(){
    return age;
  }
  
  // Overriding the compareTo method
  public int compareTo(Dog d){
    return (this.name).compareTo(d.name);
  }
  
  // Overriding the compare method to sort the age 
  public int compare(Dog d, Dog d1){
    return d.age - d1.age;
  }
  }
  
  public class Example{
  public static void main(String args[]){
    // Takes a list o Dog objects
    List&lt;Dog&gt; list = new ArrayList&lt;Dog&gt;();
  
    list.add(new Dog("Shaggy",3));
    list.add(new Dog("Lacy",2));
    list.add(new Dog("Roger",10));
    list.add(new Dog("Tommy",4));
    list.add(new Dog("Tammy",1));
    Collections.sort(list);// Sorts the array list
  
    for(Dog a: list)//printing the sorted list of names
       System.out.print(a.getDogName() + ", ");
  
    // Sorts the array list using comparator
    Collections.sort(list, new Dog());
    System.out.println(" ");
    for(Dog a: list)//printing the sorted list of ages
       System.out.print(a.getDogName() +"  : "+
       a.getDogAge() + ", ");
  }
  }
  </code></pre>