CompareTo() and compare() methods of comparator and comparable
<p>From the below code I am not understanding two things:</p>

<ol>
<li><p>The <code>CompareTo()</code> and <code>Compare()</code> methods return <code>int</code>, but how does this affect the sorting?</p></li>
<li><p>The method returns <code>this.name.compareTo(d.name)</code>. What is present in <code>this.name</code>?</p></li>
</ol>

<p>Please explain elaborately.</p>

<pre><code>import java.util.*;

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>