title: how to sort arraylist using enum type
body: <p>I have an arraylist of cards ArrayList hand;
  suppose hand={2S,3H,2D,6H,4C}. I need a method to sort the arraylist hand so that I get <code>hand = {4C,2D,3H,6H,2S}</code>. I know that enum has a method <code>compareTo()</code>, but I am not sure how to use it. Can anyone give me some guidance?</p>
  
  <pre><code>public class PlayingCard implements Comparable
   {
  //Instance Variables
  private Suit suit;
  private Rank rank;
  
  //Constructor
  public PlayingCard(Suit suit, Rank rank)
  {
      this.suit = suit;
      this.rank = rank;
  }
  ....
   public int compareTo(Object other)
   {
    PlayingCard that = (PlayingCard)other;
  
    if (this.suit == that.suit)
       return -this.rank.compareTo(((PlayingCard)other).rank);
  
    return -this.suit.compareTo(((PlayingCard)other).suit);         
   }
  
  //============================================================================
   //Representation of the Suit of a Playing-Card
  public enum Suit
  {
      CLUBS('C'), DIAMONDS('D'), HEARTS('H'), SPADES('S');
  
      private char symbol;
  
      private Suit(char symbol)
      {
          this.symbol = symbol;
      }
  
      public char getSymbol()
      {
          return this.symbol;
      }
  }
  
  //============================================================================  
   //Representation of the Rank os a Playing-Card
  public enum Rank
  {
      DEUCE('2'), TREY('3'), FOUR('4'), FIVE('5'), SIX('6'), SEVEN('7'),
      EIGHT('8'), NINE('9'), TEN('T'), JACK('J'), QUEEN('Q'), KING('K'), ACE('A');
  
      private char symbol;
  
      private Rank(char symbol)
      {
          this.symbol = symbol;
      }
  
      public char getSymbol()
      {
          return this.symbol;
      }
  }
  }
  
  --------------------------------------------------------------------------------------
  public class PlayingCardHand {
  //Instance Variables
  
  private int cardsInCompleteHand;     //Maximum # cards in this hand
  private ArrayList&lt;PlayingCard&gt; hand; //A hand of Playing-Cards
  private Comparator comparer;         //Client-provided comparison of PlayingCards
  
  //Constructor
  //Appropriate when PlayingCard compareTo() is to be used to compare PlayingCards
  public PlayingCardHand(int handSize) {
  
      if (handSize &lt; 1) {
          throw new RuntimeException("Hand size cannot be less than 1");
      }
      cardsInCompleteHand = handSize;
      hand = new ArrayList&lt;PlayingCard&gt;();
  
  }
  ...
  public void sortCards() 
  {
  }
  </code></pre>