001package algs21;
002import stdlib.*;
003import algs12.XCard;
004import java.util.Comparator;
005
006public class XSortCards2 {
007        public static void show (String title, XCard[] d) {
008                StdOut.println (title);
009                for (int i=0; i<4; i++) {
010                        for (int j=0; j<13; j++) {
011                                StdOut.format ("%3s ", d[i*13+j]);
012                        }
013                        StdOut.println ();
014                }
015                StdOut.println ();
016        }
017        private static class RankFirstComparator implements Comparator<XCard> {
018                public int compare (XCard c1, XCard c2) {
019                        if (c1.rank.compareTo (c2.rank) < 0) return -1;
020                        if (c1.rank.compareTo (c2.rank) > 0) return +1;
021                        if (c1.suit.compareTo (c2.suit) < 0) return -1;
022                        if (c1.suit.compareTo (c2.suit) > 0) return +1;
023                        return 0;
024                }
025        }
026        public static void main (String[] args) {
027                XCard[] d = XCard.newDeck ();
028                show ("Initial", d);
029                StdRandom.shuffle (d);
030                show ("Shuffled", d);
031                Insertion.sort (d);
032                show ("Sort1", d);
033                Insertion.sort (d, new RankFirstComparator ());
034                show ("Sort2", d);
035        }
036}