1

Need to write a sort method for my project.. Its a cardgame. These are the specs for the method.

  • cannot use arrays.sort() or any other external library routines.
  • sort the Hand in ascending order of Card VALUE
  • set sorted to true.
  • handle cases when arrays(hand) are not full / contain nulls. and handle the cases when the cards array is not full.

Below is the code I have so far, working but need to handle nulls and not full arrays(hand). Any suggestions or pointers would be appreciated. Thanks.

public void sortHand(){
    // loop over every slot
    for (int i = 0 ; i < cards.length; i++) {
        for (int j = i+1 ; j < cards.length; j++) {
            //getValue returns face value of card
            if(cards[j].getValue().compareTo(cards[i].getValue()) < 0) {
                //swap
                Card temp = cards[i];
                cards[i] = cards[j];
                cards[j] = temp;
            }
            else {
                isSorted = false;
            }
        }
    }
    isSorted = true;
}
10
  • 2
    what's the use of that isSorted variable? 😐 Commented Apr 29, 2017 at 2:31
  • @Mritunjay Its the sorted status of the hand.. used in other methods too.. Commented Apr 29, 2017 at 2:33
  • @NicoleYork it is a poor practice to have such a variable. Commented Apr 29, 2017 at 2:34
  • 1
    Well, which side of the array should null values be on after sorted? Commented Apr 29, 2017 at 2:34
  • The question is not clearly, please clarify it, if you just want to handle null then just use a if(cards == null) before loop. Commented Apr 29, 2017 at 2:35

1 Answer 1

4

Your code doesn't assume any particular number of cards, so it already handles non-full hands. Nothing needed there.

if(cards[j].getValue().compareTo(cards[i].getValue()) < 0)

You need to handle the cases where cards[i] or cards[j] is null. You don't want to call .getValue() on a null value; it'll throw a NullPointerException.

There are three cases to handle: one card is null, the other one is null, or both are. If they're both null you don't need to swap anything. If one is null but not the other, you need to decide if null goes first or last. In one case you'll swap and in the other you'll do nothing.

if (cards[i] == null && cards[j] == null) {
    // no swap
}
else if (cards[i] == null && cards[j] != null) {
    // swap if nulls go last, otherwise do nothing
}
else if (cards[i] != null && cards[j] == null) {
    // swap if nulls go first, otherwise do nothing
}
else if (cards[j].getValue().compareTo(cards[i].getValue()) < 0) {
    ...
}

I leave it as an exercise to combine the appropriate "swap" cases into a single if using ||. Be careful! You need to do make sure neither card is null before you call .getValue(). The order of the checks is crucial.

else {
    isSorted = false;
}

There's no need for this clause, by the way. Just set isSorted to true at the end of the method. You don't need to set it to false in the middle of sorting. Nobody's going to check the flag while you're actively sorting the hand.

Sign up to request clarification or add additional context in comments.

Comments

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.