Jump to content
  • Advertisement
Sign in to follow this  

[java] Help Please with array sorting!

This topic is 4064 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a simple console based game I am trying to make and I can't figure out how to sort this array deck[] because it is a string array. I want to be able to sort and then print them in order when they are dealt out in my main class. Can I sort the array based on the index number? If so how do you do it? OR do I cast it as a int, how can I do that with this array? Arrays always confuse me! I;d appreciate any help thanks; /code public class Cards { private String[] Deck = new String[52]; private boolean[] UsedCard = new boolean [52]; /** Creates a new instance of Cards */ public Cards() { Deck[0] = "Ace Hearts"; Deck[1] = "2 Hearts"; Deck[2] = "3 Hearts" ; Deck[3] = "4 Hearts"; Deck[4] = "5 Hearts"; Deck[5] = "6 Hearts"; Deck[6] = "7 Hearts"; Deck[7] = "8 Hearts"; Deck[8] = "9 Hearts"; Deck[9]= "10 Hearts"; Deck[10] = "Jack Hearts"; Deck[11] = "Queen Hearts"; Deck[12] = "King Hearts"; Deck[13] = "Ace Diamonds"; Deck[14] = "2 Diamonds"; Deck[15] = "3 Diamonds"; Deck[16] = "4 Diamonds"; Deck[17]= "5 Diamonds"; Deck[18]= "6 Diamonds"; Deck[19]= "7 Diamonds"; Deck[20]="8 Diamonds"; Deck[21]="9 Diamonds"; Deck[22]="10 Diamonds"; Deck[23]="Jack Diamonds"; Deck[24]="Queen Diamonds"; Deck[25]="King Diamonds"; Deck[26]="Ace Spades"; Deck[27]="2 Spades"; Deck[28]="3 Spades"; Deck[29]="4 Spades"; Deck[30]="5 Spades"; Deck[31]="6 Spades"; Deck[32]="7 Spades"; Deck[33]="8 Spades"; Deck[34]="9 Spades"; Deck[35]="10 Spades"; Deck[36]="Jack Spades"; Deck[37]="Queen Spades"; Deck[38]="King Spades"; Deck[39]="Ace Clubs"; Deck[40]="2 Clubs"; Deck[41]="3 Clubs"; Deck[42]="4 Clubs"; Deck[43]="5 Clubs"; Deck[44]="6 Clubs"; Deck[45]="7 Clubs"; Deck[46]="8 Clubs"; Deck[47]="9 Clubs"; Deck[48]="10 Clubs"; Deck[49]="Jack Clubs"; Deck[50]="Queen Clubs"; Deck[51]="King Clubs"; //init used cards to false for(int n=0; n <UsedCard.length;n++) { UsedCard[n]=false; System.out.println("" + UsedCard[n]+n); } } public String getCard() { int cardNumber; Boolean Flag = false; do { //generate a random number >0 <100 cardNumber = (int)(Math.round(Math.random()*100)); if(cardNumber <52) // if > 52 generates a new number if(UsedCard[cardNumber]==true) //test if card is allready used { Flag = false; }else // an unused card was found { UsedCard[cardNumber] = true; Flag= true; } else //random # is greater than 51 outside of the array { Flag= false; } } while(Flag==false); return Deck[cardNumber]; } public void newDeal() { for(int n=0; n<UsedCard.length;n++) { UsedCard[n]=false; } } } /code

Share this post

Link to post
Share on other sites

"All arrays" can be sorted through the class Arrays, which has static methods to sort arrays of different kinds. The general rule for sorting arrays with Objects (which is what String is), is that each element of the array must be comparable to other elements. The String class meets this requirement by implementing the Comparable interface which guarantees that String has the method "compareTo(String otherString)". However, by default, the sorting methods tries to sort the array through "Natural order". For numbers, this means 1,2,3,4 etc. For Strings, this means "a","b","c","d" and so on. Since String is immutable, you cannot change the way your Strings will be compared to one another directly.


The sorting method for Objects in the Array class is also overloaded with a signature that takes a Comparator as well. The Comparator interface defines two methods, "compare(T a, T b)" and "equals(Object obj)". So you can make your own Comparator, to override sorting as you please.

Since Strings are well, just Strings, it might be difficult for you to perform complex sorting on them. A suggestion might be to introduce a Card class, and a Value and Suit enum.

For instance:

public class Card
public enum Suit

public enum Value

private Value cardValue;
private Suit cardSuit;

public Card(Suit _cardSuit, Value _cardValue)
cardSuit = _cardSuit;
cardValue = _cardValue;

public String toString()
return cardValue.name() + cardSuit.name();

Hope this helps. In any case, look up the javadocs for "Arrays" "Comparator" and "Comparable".

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!