001package algs21;
002import stdlib.*;
003import java.util.Comparator;
004
005public class XSortCards0 {
006        public static void show (String title, String[] d) {
007                StdOut.println (title);
008                for (int i=0; i<4; i++) {
009                        for (int j=0; j<13; j++) {
010                                StdOut.format ("%3s ", d[i*13+j]);
011                        }
012                        StdOut.println ();
013                }
014                StdOut.println ();
015        }
016        private static class CardComparator implements Comparator<String> {
017                private static char suit (String s) {
018                        return s.charAt (s.length () - 1);
019                }
020                private static int rank (String s) {
021                        char rank = s.charAt (0);
022                        switch (rank) {
023                        case '1' : return 10;
024                        case 'J' : return 11;
025                        case 'Q' : return 12;
026                        case 'K' : return 13;
027                        case 'A' : return 14;
028                        default: return rank - '0';
029                        }
030                }
031                public int compare (String c1, String c2) {
032                        if (suit(c1) < suit(c2)) return -1;
033                        if (suit(c1) > suit(c2)) return +1;
034                        if (rank(c1) < rank(c2)) return -1;
035                        if (rank(c1) > rank(c2)) return +1;
036                        return 0;
037                }
038        }
039        public static void main (String[] args) {
040                String[] d =  {
041                                "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", "AC",
042                                "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", "AD",
043                                "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", "AH",
044                                "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", "AS" };
045                show ("Initial", d);
046                StdRandom.shuffle (d);
047                show ("Shuffled", d);
048                Insertion.sort (d);
049                show ("Sort1", d);
050                Insertion.sort (d, new CardComparator ());
051                show ("Sort2", d);
052                
053                String c1 = "@E";
054                d[0] = c1;
055                StdRandom.shuffle (d);
056                Insertion.sort (d, new CardComparator ());
057                show ("Sort3", d);
058        }
059}