Ryan Sayles
Ryan Sayles

Reputation: 3431

Sorting Card objects

I'm creating a simple card game and I want to be able to sort an ArrayList<Card> so that the players hand is easier to read. Right now I have:

Card class:

public class Card implements Comparable {

public enum Suits {
    SPADES,
    CLUBS,
    HEARTS,
    DIAMONDS
}

public enum Values {
    TWO,
    THREE,
    FOUR,
    FIVE,
    SIX,
    SEVEN,
    EIGHT,
    NINE,
    TEN,
    JACK,
    QUEEN,
    KING,
    ACE, 
}

private Suits suit;
private Values value;

public Card(final Values value, final Suits suit) {
    this.value = value;
    this.suit = suit;
}

public Values getValue() {
    return value;
}

public Suits getSuit() {
    return suit;
}

public void showCard() {
    value = getValue();
    suit = getSuit();

    System.out.println(value + " of " + suit);
}

public int compareTo(final Object compareCard) {
    Card compareToCard = (Card) compareCard;
    Suits thisSuit = this.suit;
    Values thisValue = this.value;

    if (thisSuit.ordinal() == compareToCard.getSuit().ordinal() &&
        thisValue.ordinal() == compareToCard.getValue().ordinal()) {
        return 0;
    }
    else if (thisSuit.ordinal() == compareToCard.getSuit().ordinal()) {
        if (thisValue.ordinal() > compareToCard.getValue().ordinal()) {
            return 1;
        }
        else {
            return -1;
        }
    }
    else {
        return -1;
    }
}

}

I used the ordinal values since I want to order the Values 2-Ace and it doesn't really matter which order the Suits are in as long as they are grouped together.

Then I have an ArrayList<Card> called playerHand. I am trying to sort using:

playerHand.sort();

When I go to compile this code I am receiving an error:

error: method sort in interface List<E> cannot be applied to given types;
    playerHand.sort();
              ^
  required: Comparator<? super Card>
  found: no arguments
  reason: actual and formal argument lists differ in length
    where E is a type-variable:
    E extends Object declared in interface List

I'm not really sure what I am doing wrong. Any help would be greatly appreciated!

Upvotes: 1

Views: 1755

Answers (3)

Robert
Robert

Reputation: 2669

The error message is quite informative.

required: Comparator<? super Card>
found: no arguments

Method sort in interface List expects an argument but you didn't pass one. This argument can either be a Comparator or null and tells the method how to sort the list.

You already implemented the interface Comparable. But this only defines the natural order of the cards. If you want to sort by this order you either pass Comparator.naturalOrder() or simply null. But if you wanted to sort the cards in reverse natural order you would pass Comparator.reverseOrder().

You could even go so far and create two separate SuitComparator implements Comparator<Card> and ValueComparator implements Comparator<Card> and be able to combine these in different ways. For example the following would recreate the natural order:
new SuitComparator().thenComparing(new ValueComparator())

Upvotes: 0

niceman
niceman

Reputation: 2673

define this class outside your card class:

class CardComparator implements Comparator<Card>
{

    @Override
    public int compare(Card o1, Card o2) 
    {
         if (o1.getSuit().ordinal() == o2.getSuit().ordinal() && o1.getValue().ordinal() == o2.getValue().ordinal()) 
         {
             return 0;
         }
         else if (o1.getSuit().ordinal() ==o2.getSuit().ordinal()) 
         {
             if (o1.getValue().ordinal() > o2.getValue().ordinal())
                 return 1;                 
             else                  
                 return -1;                
         }
         else
             return -1;    
    }

}

and then call the sort method like this:

playerHand.sort(new CardComparator());

just don't forget to import java.util.Comparator.

Upvotes: 1

Iłya Bursov
Iłya Bursov

Reputation: 24146

you need to implement Comparable interface, like this:

public class Card implements Comparable<Card> {

Upvotes: 1

Related Questions