title: How to use the Comparator interface in Java?
body: <p>I can't figure out how to properly use the Comparator interface.</p>
  
  <p>NOTE: I don't want to use the "one line" comparator implementation I see on most code, meaning :</p>
  
  <pre><code>Collections.sort(deck, new Comparator&lt;Card&gt;() {
    @Override
    public int compare(Card o1, Card o2) {
     return WHATEVER;
    });
  </code></pre>
  
  <p>Again, I don't want to use this, because I'm going to be sorting many different ArrayLists and it seems like a waste of space to do that every time. I want to somehow make it work, having my compareTo method written once in one place.</p>
  
  <p>I tried many different things, but I'm new to implementing interfaces, so I'm probably missing something.</p>
  
  <p>This is my Card class with it's compare method. (NOTE: Let me save you some time and say that I'm not interested in using Enumerations to simplify the code and the comparison, I just want to learn how to use Comparator properly. I know it looks bad but I have to use some already written code. )</p>
  
  <pre><code>import java.util.*;
  public class Card implements Comparator&lt;Card&gt;
  {
      private String number;
      private int value;
      private int finalValue;
      private String suit;
      private Player owner;
  
      public Card(String number, String suit)
      {
          this.number=number;
          switch (number){
              case "2": this.value = 2;
                  break;
              case "3": this.value = 3;
                  break;
              case "4": this.value = 4;
                  break;
              case "5": this.value = 5;
                  break;    
              case "6": this.value = 6;
                  break;
              case "7": this.value = 7;
                  break;
              case "8": this.value = 8;
                  break;    
              case "9": this.value = 9;
                  break;
              case "10": this.value = 10;
                  break;
              case "J": this.value = 11;
                  break;
              case "Q": this.value = 12;
                  break;
              case "K": this.value = 13;
                  break;
              case "A": this.value = 14;
                  break;
              }
  
          this.suit=suit;
          switch (suit){
              case "Spades": this.finalValue = this.value*4;
                  break;
              case "Hearts": this.finalValue = this.value*5;
                  break;
              case "Clubs": this.finalValue = this.value*2;
                  break;
              case "Diamonds": this.finalValue = this.value*3;
                  break;
              }
      }
  
      public int compare(Card card1, Card card2)
      {
          Integer suitValue1;
          if (card1.getSuit() == "Hearts")
          suitValue1=5;
          else if (card1.getSuit() == "Spades")
          suitValue1=4;
          else if (card1.getSuit() == "Diamonds")
          suitValue1=3;
          else
          suitValue1=2;
  
          Integer suitValue2;
          if (card2.getSuit() == "Hearts")
          suitValue2=5;
          else if (card2.getSuit() == "Spades")
          suitValue2=4;
          else if (card2.getSuit() == "Diamonds")
          suitValue2=3;
          else
          suitValue2=2;
  
          Integer value1, value2;
          value1 = card1.getValue();
          value2 = card2.getValue();
  
          if (suitValue1 != suitValue2)
          return suitValue1.compareTo(suitValue2);
          else
          return value1.compareTo(value2);
  
      }
  
      //get methods
      public int getValue()
      {return this.value;}
  
      public int getFinalValue()
      {return this.finalValue;}
  
      public String getNumber()
      {return this.number;}
  
      public String getSuit()
      {return this.suit;}
  
      public Player getOwner()
      {return this.owner;}
  
      //set methods
      public void setValue(int value)
      {this.value = value;}
  
      public void setFinalValue(int finalValue)
      {this.finalValue = finalValue;}
  
      public void setNumber(String number)
      {this.number = number;}
  
      public void setSuit(String suit)
      {this.suit = suit;}
  
      public void setOwner(Player player)
      {this.owner = player;}
  
  }
  </code></pre>
  
  <p>So that's my compare method. Should I put it in another class or something? I try to call it using this:</p>
  
  <pre><code>Collections.sort(deck, new Comparator&lt;Card&gt;());
  </code></pre>
  
  <p>but that is obviously wrong.</p>
  
  <p>deck is an ArrayList : <code>private ArrayList&lt;Card&gt; deck = new ArrayList&lt;Card&gt;();</code> filled with various Card objects.</p>