Ryan Sayles
Ryan Sayles

Reputation: 3441

Order a list of objects

I am making a program that simulates a card game. I have a class Card that represents a playing card:

//suit types
public enum Suits{
    SPADES,
    CLUBS,
    HEARTS,
    DIAMONDS
}

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

private Suits suit;
private Values value;

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

In my game class I have a players hand represented by a List of Cards:

public List<Card> playerHand = new ArrayList<Card>();

Is there a way that I can sort the players hand by my enum Values? Meaning if a players hand is:

TEN, ACE, TWO, FIVE

After ordering it would be:

TWO, FIVE, TEN, ACE

My conditions would be Ace is always high, and Suit doesn't matter. I know ordering Lists are trivial I just don't know how to do it for an Object based on an enum

Upvotes: 0

Views: 112

Answers (4)

Thomas
Thomas

Reputation: 88707

Enums have a default ordering, i.e. they implement Comparable and thus you can just sort a list of enums or call ACE.compareTo( TEN ) in some Comparator. Note that the order of the enum values is the order of definition in the enum class (based on ordinal, see next paragraph), i.e. in your case TWO < THREE < FOUR etc.

Additionally enums have an ordinal, i.e. the index of the enum value inside the definition. In your case TWO would have the ordinal 0, THREE the ordinal 1 etc.

In order to sort your cards, you could use a Comparator (or implement Comparable<Card>) like this:

class CardComparator implements Comparator<Card> {
  public int compare( Card lhs, Card rhs) {
    //suit would be relevant otherwise you'd end up with spades 10, hearts 10, spades jack etc.
    //thus sort by suit first to keep the cards of a suit together
    int r = lhs.suit.compareTo( rhs.suit ); 

    if( r == 0) { //same suit    
      r = lhs.value.compareTo( rhs.value );
    }
    return r;
  } 
}

Upvotes: 6

TheWalkingCube
TheWalkingCube

Reputation: 2116

You can implement the interface Comparable. You would have to redefine the compareTo method for your Card class and it would then allow you to do something like :

Card a = ...;
Card b = ...;

if(a.compareTo(b) <= 0) {
    ...
}

Or even :

Collections.sort(list);

See http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

Upvotes: 0

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

I would set a value for each of the Suits and Values, so that I can compare them to one another. For example:

public enum Suits{
    SPADES(1),
    CLUBS(2),
    HEARTS(3),
    DIAMONDS(4);

    private int value;

    private Suits(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

and the Values enum:

public enum Values{
    TWO(2),
    THREE(3),
    FOUR(4),
    FIVE(5),
    SIX(6),
    SEVEN(7),
    EIGHT(8),
    NINE(9),
    TEN(10),
    JACK(11),
    QUEEN(12),
    KING(13),
    ACE(14);

    private int value;

    private Values(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

Then, you can implement a Comparator for the Cards:

public class CardsComparator implements Comparator<Card> {
    public int compare(Card c1, Card c2) {
        //implementation based on the values of Suits and Values.
        if (c1.getSuits().getValue() == c2.getSuits.getValue()) {
            return c1.getValue().compareTo(c2.getValue());
        } else {
            return c1.getSuits().compareTo(c2.getSuits());
        }
    }
}

and apply an instance of the Comparator to sort your List<Card>:

Collections.sort(listOfCards, new CardsComparator());

Upvotes: 1

Denis Kulagin
Denis Kulagin

Reputation: 8906

Use

Collections.sort(list, comparator);

for sorting. As for comparator:

new Comparator<Card>() {
  @Override
  public int compare(Card x, Card y) {
    return x.getValue().compareTo(y.getValue());
  }
}

Upvotes: 1

Related Questions