Search code examples
javaarraysstringsorting

How do I sort 13 random playing cards in order (sequence) in java?


I want to order 13 playing cards with order clubs, diamonds, hearts and spades, sequence 2,3,4,5,6,7,8,9,10,J,Q,K,A. For example 4C means "4 Club".

I tried using the code below to order them with sample player north like this

4H 5H 8C 8D 9H 9D AC 6S 5D 6D KD 10C AD

the result are

8C AC 10C 5D 6D 8D 9D AD KD 4H 5H 9H 6S

I expected the result

8C 10C AC 5D 6D 8D 9D KD AD 4H 5H 9H 6S

Here is the code


import java.util.ArrayList;

public class Test {

    public static void main(String[] args) {
        String[] sTest = {"4H", "5H", "8C", "8D", "9H", "9D", "AC", "6S", "5D", "6D", "KD", "10C", "AD" };
        
        for (int i=0;i<13;i++) {
            System.out.print(sTest[i]+" ");
        }
        System.out.println();
        System.out.println("Sorting");
        ArrayList<String> tmp = sortEnd2Begin(sTest);
        tmp.forEach((String myCard) ->{
            System.out.print(myCard+" ");
        });
        System.out.println();
    }
    
    private static ArrayList<String> sortEnd2Begin(String[] myCards) {
        ArrayList<String> myNewCards = new ArrayList<>();
        myNewCards.add(myCards[0]);
        for (int i=1;i<myCards.length;i++) {
            // loop for unordered myCards
            String card2Sort = myCards[i];
            for (int j=0;j<myNewCards.size();j++) {
                // loop for ordered myNewCards
                // find until card2Sort equal or bigger then existing list
                String cardIterate = myNewCards.get(j);
                int iterMax = card2Sort.length();
                if (card2Sort.length() > cardIterate.length()) {
                    iterMax = cardIterate.length();
                }
                int posCard = beforeOrAfter(card2Sort, cardIterate, 0, iterMax);
                if (posCard==0 || posCard<0) {
                    myNewCards.add(j, card2Sort);
                    break;
                } else {
                    // TODO 
                    //System.out.println(j); // debug
                    if (j+1==myNewCards.size()) {
                        // end of list
                        myNewCards.add(card2Sort);
                        break;
                    }
                }
            }

        }
        return myNewCards;
    }
    
    // -1 wordA before wordB
    // 0 wordA equal wordB
    // 1 wordA after wordB
    private static int beforeOrAfter(String wordA, String wordB, int iterX, int iterMax) {
        //System.out.println(wordA+" "+wordB+" "+iterX+" "+iterMax);
        int inA = wordA.length()-1-iterX;
        int inB = wordB.length()-1-iterX;
        char cA = wordA.charAt(inA);
        char cB = wordB.charAt(inB);
        int retValue = 0;
        if (iterX+1==iterMax) {
            // we reach max recursive {
            //System.out.println(iterX+" "+iterMax+" "+cA+" "+cB); // debug
            if (cA==cB) {
                //System.out.println("Debug"); // debug
                if (wordA.length()<wordB.length())  {
                    retValue = -1;
                } else if (wordA.length()>wordB.length())  {
                    retValue = 1;
                } else {
                    retValue = 0; // equal letters and length
                }
            } else if (cA<cB) {
                //retValue = -1;  // ori
                if (wordA.length()>wordB.length()) {
                    retValue = 1;
                } else {
                    retValue = -1;
                }
            } else if (cA>cB) {
                retValue = 1; // ori
            }
        } else {
            if (cA<cB) {
                //System.out.println(cA+" < "+cB+" "+(cA < cB)); // debug
                retValue = -1; // ori
            } else if (cA>cB) {
                //System.out.println(cA+" > "+cB+" "+(cA > cB)); // debug
                retValue = 1; // ori

            } else {
                //System.out.println(cA+" = "+cB+" "+(cA == cB)); // debug
                retValue = beforeOrAfter(wordA, wordB, iterX+1, iterMax);
            }
        }
        return retValue;
    }
}

Edited 20240708

function sortEnd2Begin uses to sort string array and return ArrayList as a sorted array. function sortEnd2Begin will iterate myCards array, and put it into new arraylist in correct order.

to get correct order in inserting or append arraylist, I use function beforeOrAfter to compare which is lower, equal or higher. if it is lower or equal, it will insert in the begin or middle of arraylist. if end of arraylist reach, it will append the value.

function beforeOrAfter will compare char to char from end of word to determine the word equal, lower or higher order base on ASCII table. function beforeOrAfter will recursive until minimum length of word reached, and determine (return value) -1 (lower), 0 (equal) or 1 (higher).

functions sortEnd2Begin and beforeOrAfter work like sorting file name in file manager, but in reverse order.


Solution

  • Suite C, D, H and S are in ASCII order.

    The value of suite 2 - 10, J, Q, K and A are not. I remap 10 to A, J to B, Q to C, K to D and A to E to make it ASCII order correctly.

    The complete source code to shuffle and distribute cards https://dedetoknotes.blogspot.com/2024/07/java-17-shuffle-52-playing-cards-and.html

    the code are

    ...
                //int posCard = beforeOrAfter(card2Sort, cardIterate, 0, iterMax);
                int posCard = beforeOrAfter2(card2Sort, cardIterate);
    
    ...
    /* -1 wordA before wordB
     * 0 wordA equal wordB
     * 1 wordA after wordB
     * suite order less C D H S higher
     * value suit  less 0 1 2 3 4 5 6 7 8 9 A J K Q
     * map 10 to A
     *      J to B
     *      Q to C
     *      K to D
     *      A to E
     */
    private static int beforeOrAfter2(String wordA, String wordB) {
        //System.out.print("word "+wordA+" "+wordB+" "); // debug
        char charA = wordA.charAt(wordA.length()-1);
        char charB = wordB.charAt(wordB.length()-1);
        //System.out.println(wordA+" "+wordB+" End "+charA+" "+charB+" "); // debug
        if (charA<charB) {
            return -1;
        } else if (charA>charB) {
            return 1;
        } else {
            // equal
            charA = wordA.charAt(wordA.length()-2);
            charB = wordB.charAt(wordB.length()-2);
            // mapping charA to correct order
            if (charA=='0') {
                charA='A';
            } else if (charA=='J') {
                charA='B';
            } else if (charA=='Q') {
                charA='C';
            } else if (charA=='K') {
                charA='D';
            } else if (charA=='A') {
                charA='E';
            }
            // mapping charB to correct order
            if (charB=='0') {
                charB='A';
            } else if (charB=='J') {
                charB='B';
            } else if (charB=='Q') {
                charB='C';
            } else if (charB=='K') {
                charB='D';
            } else if (charB=='A') {
                charB='E';
            }
            
            //System.out.println(" First "+charA+" "+charB); // debug
            if (charA<charB) {
                return -1;
            } else if (charA>charB) {
                return 1;
            }
        }
        
        return 0;
    }