-1

I am trying to rank an array of sorted poker cards, but am unable to find a way to make sure the order of rank alphabets attached to the suits to go as (A-K-Q-J-T), for example my output shows HT (heart ten) to be bigger than HJ (heart joker) when it should be the other way round.

private void sort2D (String [][] twoD)
    {
        // to compile sorted array
        ArrayList<String> toCompileSorted = new ArrayList<String> ();

        for (int row = 0; row < twoD.length; row++)
        {
            String [] sorted2D = new String [MAXC]; // MAXC = 13
            for (int column = 0; column < twoD[row].length; column++)
            {
                sorted2D [column] = twoD [row][column];
            }
            // 2d array becomes ascending only row by row, not everything
            Arrays.sort (sorted2D);
            // make sure it goes by A-K-Q-J-T ???
            // array split according to letter ranks ??

            for (String s : sorted2D)
            {
                toCompileSorted.add (s);
            }
        }
        // put back into 2d array
        int i = 0;
        for (int row = 0; row < twoD.length; row++)
        {
            for (int column = 0; column < twoD[row].length; column++)
            {
                    twoD [row][column] = toCompileSorted.get(i);
                    i++;
            }
        }
    }
C6 C7 CJ CT D8 H2 HJ HT S3 S5 S6 S7 SA 
5 - 3 - 1 - 4
C2 C3 C8 CK D4 D7 DK DQ H3 H7 HA HK SQ 
1 - 4 - 4 - 4
C4 C9 CA CQ D2 D5 H5 H9 HQ S2 S8 S9 SJ 
4 - 3 - 2 - 4
C5 D3 D6 D9 DA DJ DT H4 H6 H8 S4 SK ST 
3 - 3 - 6 - 1

This is my result (ignore the numbers below each row), in the first row, CJ (club joker) is bigger than CT (club ten) but the output shows otherwise, same thing happens for other rows too.

Draken
  • 3,134
  • 13
  • 34
  • 54
WickedLook
  • 15
  • 4
  • 3
    Don't use Strings to represent card values. Use an enum, where the values are defined in the appropriate order. https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html – JB Nizet Oct 09 '19 at 16:40
  • @JB Nizet, I do have enum values defined and separated them into Suits and Ranks, if i declare an enum rank set as follows enum { TEN ('T'), JOKER ('J')}, would java treat 'J' to be bigger than 'T' then? Just a question because your answer got me thinking abit – WickedLook Oct 09 '19 at 16:48
  • 1
    Ordinal numbers are assigned in order of declaration of enum members. –  Oct 09 '19 at 16:51
  • It wouldn't treat 'J' to be bigger than 'T'. But Rank.JOKER would be bigger than Rank.TEN, because JOKER is declared after TEN in the enum. 'J' and 'T' are characters, not instances of Rank.. – JB Nizet Oct 09 '19 at 16:56
  • This is question is very specific, making it not minimal enough IMO – GalAbra Oct 10 '19 at 05:52
  • If i were to create an arrayList of Enum type and store the enum constants inside, would i be able to compare with the > / < operators for enum constants in the arrayList of Enum type? – WickedLook Oct 10 '19 at 10:06

1 Answers1

0

Quick Solution

Make a custom comparator

public class CardComperator implements Comparator<String> {
    private static final Map<Character, Integer> cMap = new HashMap<>();
    private static final Map<Character, Integer> sMap = new HashMap<>();

    static {
        sMap.put('C', 1);
        sMap.put('D', 2);
        sMap.put('H', 3);
        sMap.put('S', 4);

        cMap.put('A', 1);
        cMap.put('K', 2);
        cMap.put('Q', 3);
        cMap.put('J', 4);
        cMap.put('T', 5);
        // so on
    }

    @Override
    public int compare (String p1, String p2) {
        int r1 = sMap.get(p1.charAt(0)) * 100 + cMap.get(p1.charAt(1));
        int r2 = sMap.get(p2.charAt(0)) * 100 + cMap.get(p2.charAt(1));
        return r1 - r2;
    }
}

Pass the comparator as the second parameter of Arrays.sort (sorted2D)

Arrays.sort (sorted2D, new CardComperator());

.

Better Solution

Here is the solution I applied in my first job's pre-interview task.

List<Card> mCards = ...
Collections.sort(mCards, new CardComperator());

public class Card {

    private CardRank mCardRank;
    private CardSuit mCardSuit;

    public Card(CardSuit cardSuit, CardRank cardRank) {
        this.mCardRank = cardRank;
        this.mCardSuit = cardSuit;
    }

    public int getCardRankToInt() {
        return mCardSuit.ordinal() * 100 + mCardRank.getCardinal();
    }
    // getters, setters
}

public enum CardRank {

    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 cardinal;

    private CardRank(final int cardinal) {
        this.cardinal = cardinal;
    }

    public int getCardinal() {
        return cardinal;
    }
}

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

public class CardComperator implements Comparator<Card> {

    @Override
    public int compare(Card p1, Card p2) {
        // p2.getCardRankToInt() - p1.getCardRankToInt() for decending order
        return p1.getCardRankToInt() - p2.getCardRankToInt();
    }
}
Roaim
  • 2,298
  • 2
  • 11
  • 23