package oving5.card; import java.util.Comparator; public class CardComparator implements Comparator { /* * @return The index (value) of the suit as an Integer. */ private int getSuitValue(Card c) { return "CDSH".indexOf(c.getSuit()); } /* * Compares two cards. * * @param c1 The first card. * @param c2 The second card. * @return A negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second, respectively. */ public int compare(Card c1, Card c2) { if (c1.getFace() == c2.getFace()) { if ((getSuitValue(c1) - getSuitValue(c2)) < 0) return -1; if ((getSuitValue(c1) - getSuitValue(c2)) > 0) return 1; return 0; } else { if ((c1.getFace() - c2.getFace()) > 0) return -1; if ((c1.getFace() - c2.getFace()) < 0) return 1; return 0; } } }