1

I have this code that sorts my arrayLists for different card suites (clubs, diamonds, hearts, and spades). Is there any way to write the following code so I do not have to write it for each ArrayList?

        String clubsLabel = "Clubs";
        String diamondsLabel = "Diamonds";
        String heartsLabel = "Hearts";
        String spadesLabel = "Spades";

        Collections.sort(clubs, new cardIdSorter());
        System.out.printf("%-12s", clubsLabel);
        for(PlayingCard clubsCard: clubs) {
            System.out.print(clubsCard);
        }

        System.out.println(" ");

        Collections.sort(diamonds, new cardIdSorter());
        System.out.printf("%-12s", diamondsLabel);
        for(PlayingCard diamondsCard: diamonds) {
            System.out.print(diamondsCard);
        }

        System.out.println(" ");

        Collections.sort(hearts, new cardIdSorter());
        System.out.printf("%-12s", heartsLabel);
        for(PlayingCard heartsCard: hearts) {
            System.out.print(heartsCard);
        }

        System.out.println(" ");

        Collections.sort(spades, new cardIdSorter());
        System.out.printf("%-12s", spadesLabel);
        for(PlayingCard spadesCard: spades) {
            System.out.print(spadesCard);
        }
jdc987
  • 743
  • 3
  • 10
  • 20
  • Can you maintain a sorted List as you insert the elements? See: http://stackoverflow.com/questions/4031572/sorted-array-list-in-java – Eric Levine Oct 09 '13 at 13:19

4 Answers4

2

Write a custom method and call for each collection.

public static void sortAndPrintCollection(Collection col,String var){   
Collections.sort(col, new cardIdSorter());
        System.out.printf("%-12s", var);
        for(PlayingCard p: col) {
            System.out.print(p);
        }}

Example of usage

ClassName.collectionSorter(clubs,clubsLabel);
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

Define a method like this -

public void sortAndPrint(ArrayList<PlayingCard> cards, String label) {
    Collections.sort(cards, new cardIdSorter());
    System.out.printf("%-12s", label);

    for(PlayingCard card: cards) {
        System.out.print(card);
    }
}

then call it like this -

sortAndPrint(clubs, "Clubs");
sortAndPrint(diamonds, "Diamonds");
sortAndPrint(hearts, "Hearts");
sortAndPrint(spades, "Spades");
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
1

Separate your code into a method:

void sortAndPrint(List<PlayingCard> cards, String label) {
    Collections.sort(cards, new cardIdSorter());
    System.out.printf("%-12s", label);
    for(PlayingCard card: cards) {
        System.out.print(card);
    }
}

Then call the method once for each set of cards:

sortAndPrint(clubs, clubsLabel);
sortAndPrint(diamonds, diamondsLabel);
sortAndPrint(hearts, heartsLabel);
sortAndPrint(spades, spadesLabel);
Armand
  • 23,463
  • 20
  • 90
  • 119
0

I haven't tested it, but you could probably do something like this.

Map<> suites = new HashMap<String, ArrayList<PlayingCard>>() {{
    put("Clubs",clubs); put("Diamonds",diamonds); put("Hearts",hearts); put("Spades",spades);
}};

for (Map.Entry<String, ArrayList<PlayingCard>> entry : suites.entrySet()) {
    Collections.sort(entry.getValue(), new cardIdSorter());
    System.out.printf("%-12s", entry.getKey());
    for(PlayingCard card: entry.getValue()) {
        System.out.print(card);
    }
}

Welcome to Java, where refactoring your code to remove duplication often just makes it even more ugly. Note that this will create a new inner class to do the map initialization.

Antimony
  • 37,781
  • 10
  • 100
  • 107