Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

303
Views
Sorting Arrays in Java and handle any null values

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;
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!