title: How to write a Comparator
body: <pre><code>public class Date {
      private int month; // must be 1-12
      private int day; // must be 1-31
  
      public int getMonth() {return month;}
  
      public int getDay() {return day;}
  
      public Date(int m, int d) {
          if (m &gt;= 1 &amp;&amp; m &lt;= 12)
              month = m;
          else
              month = 1;
  
          if (d &gt;= 1 &amp;&amp; d &lt;= 31)
              day = d;
          else
              day = 1;
      } // end constructor
  } // end class Date
  </code></pre>
  
  <p>Comparator Class</p>
  
  <pre><code>import java.util.*;
  public class DateComparator implements Comparator &lt;Date&gt;{
      public int compare(Date date1, Date date2){
              if (date1.getMonth() &gt; date2.getMonth()){
                  return 1;
              }
              else if(date1.getMonth() &lt; date2.getMonth()){
                  return -1;
              }
              else{ //if(date1.getMonth() == date2.getMonth()){
                  if (date1.getDay() &gt; date2.getDay()){
                      return 1;
                  }
                  if (date2.getDay() &lt; date2.getDay()){
                      return -1;
                  }
                  else{// (date2.getDay() == date2.getMonth()){
                      return 0;
                  }
              }
          }
      }
  </code></pre>
  
  <p>I'm trying to write a comparator for this date class, and I was wondering if this was the correct way to do it. Any advice would be much appreciated!</p>