2

I need to know how to sort an array of Strings, I know how to do a selection sort, however I have BOTH numbers and letters in the array. The idea is to sort a hand of cards. This is what I have ...

clubsArry[0] = 7C

clubsArry[1] = AC

clubsArry[2] = TC

clubsArry[3] = KC

The second character represents the suit of the card, in this case clubs, and the first character is the card value (the T represents Ten, K is king, A is ace, etc.), so for the clubsArry[0] , the card is a 7 of clubs. When I sort it and print, the output I get is ....

7C

AC

KC

TC

So all the cards that that have a letter in the first position get sorted properly but I still have the 7 of clubs that is at the top of the output. The output that I want is ...

AC

KC

TC

7C

The way I tried to sort it was by doing this:

public void sortClubs() // A selection sort, that will sort out the clubs 
{
    for (int i = 1; i < clubsArry.length; i++)
    {
        int s = i-1;
        for (int j = i; j < clubsArry.length; j++)
        {
            if (clubsArry[j].compareTo(clubsArry[s]) < 0)
            {
                 s = j;
            }
        }
         String temp = clubsArry[i-1];
         clubsArry[i-1] = clubsArry[s];
         clubsArry[s] = temp;
    }
}

I just cant seem to figure it out, I was going to convert it into a char array and then cast into an int and the sort and convert it all back, but I figure there must be a simpler way to do this, If there is a way at all.

Thanks for all the help :)

MadcapClover

1
  • So is Ace a 1 or 11? lol (also, refer to the Episode of Chuck) Commented Mar 31, 2011 at 21:29

4 Answers 4

3

Use a Comparator, then the Arrays.sort() method. This should help with your coding as it lets you focus on a single thing: how do I order 2 different cards? You will have to hard-code in the ordering somehow, here is one way to do it. I haven't tested it; and it only sorts based on the first character. If the first characters are the same, then you should look at the second charater.

import java.util.Comparator;

public class CardComparator implements Comparator<String> {

    private static final List<Character> CARD_ORDER = Arrays.asList( 
    'A', 'K', 'Q', 'J', '1', '9', '8', '7', '6', '5', '4', '3', '2');

    @Override
    public int compare(String card1, String card2) {
        if( card1.equals(card2)) {
            return 0;
        }

        int firstIndex = CARD_ORDER.indexOf(card1.charAt(0));
        int secondIndex = CARD_ORDER.indexOf(card2.charAt(0));

        return firstIndex - secondIndex;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 When I first saw OP:s example, this was the EXACT solution I thought of for the same reason "concentrate on sorting 2 items" =)
1

Have a look at this great link related to array sorting: http://www.leepoint.net/notes-java/data/arrays/70sorting.html Arrays class has few static methods very useful maybe some of them suit your needs.

Comments

1

You can use String::charAt(int) to retrieve the characters at positions 0 and 1 and use those to compare.

Comments

1

Use Collections to do this for you. A sorted collection like a TreeMap will sort as you fill it. Collections with a sort method can be sorted after loading.

If you need a custom sort order, you can provide your own Comparator. Arrays allow you to provide your own Comparator. TreeMaps can be created with a Comparator.

If you create your a class you wish to be sortable you should implement the Comparable interface.

2 Comments

Correct in providing your own comparator, however you can't subclass string (it's final). You can create your own string comparator, though, you don't need a custom class to do that.
@Steve B: Thanks, I haven't needed to override comparator. Updated post accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.