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;
}
isSortedvariable? 😐if(cards == null)before loop.