Reputation: 29
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
Upvotes: 2
Views: 6623
Reputation: 5930
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;
}
}
Upvotes: 3
Reputation: 7576
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.
Upvotes: 1
Reputation: 23373
You can use String::charAt(int) to retrieve the characters at positions 0 and 1 and use those to compare.
Upvotes: 1
Reputation: 12423
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.
Upvotes: 1