title: How to implement Comparator using compareTo()
body: <p>I have a class, <code>Card</code>:</p>
  
  <pre><code>public class Card implements Comparable&lt;Card&gt; {
      public enum rank {TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE};
      private rank Rank;
      public enum suit {CLUBS, DIAMONDS, HEARTS, SPADES};
      private suit Suit;
  }
  </code></pre>
  
  <p>Which I need to perform two tasks on. First - <strong><em>Make the Card class Comparable, so that compareTo can be used to sort the cards into ascending order.</em></strong> I've done this here:</p>
  
  <pre><code>@Override
  public int compareTo(Card other) {
      if(this.Rank != other.Rank) {
          if(this.Rank.ordinal() &gt; other.Rank.ordinal()) {
          return 1;
          }
          else if(this.Rank.ordinal() &lt; other.Rank.ordinal()) {
              return -1;
          }
          else {
              return 0;
          }
      }
      else {
          if(this.Suit.ordinal() &gt; other.Suit.ordinal()) {
          return 1;
          }
          else if(this.Suit.ordinal() &lt; other.Suit.ordinal()) {
              return -1;
          }
          else {
              return 0;
          }
      }
  }
  </code></pre>
  
  <p>and secondly - <strong><em>Add a Comparator class as a nested classes of the Card class called CompareDescending. This should be used to sort the cards into descending order.</em></strong> I've done this here, and it works well:</p>
  
  <pre><code>public static class CompareDescending implements Comparator&lt;Card&gt; {
      @Override
      public int compare(Card card, Card other) {
          if(card.Rank != other.Rank) {
              if(card.Rank.ordinal() &lt; other.Rank.ordinal()) {
              return 1;
              }
              else if(card.Rank.ordinal() &gt; other.Rank.ordinal()) {
                  return -1;
              }
              else {
                  return 0;
              }
          }
          else {
              if(card.Suit.ordinal() &lt; other.Suit.ordinal()) {
              return 1;
              }
              else if(card.Suit.ordinal() &gt; other.Suit.ordinal()) {
                  return -1;
              }
              else {
                  return 0;
              }
          }
      }
  }
  </code></pre>
  
  <p>However, I wondered if I've gone about this the wrong way. Should my nested Comparator class use the compareTo() function inside it? Is this better practice?</p>