How to sort an array by two different indices?

Asked

Viewed 41 times

0

Hello to all recently wrote this code, where it will show the list of students in the array by decreasing order of score and at the end shows the top 3. This is the code:

<?php

$Turma = array(

array("nome" => "Diogo", "score" => "100", "time" => "6" ),
array("nome" => "Joao","score" => "500", "time" => "3" ),
array("nome" => "Miguel", "score" => "125", "score" => "8" ),
array("nome" => "Daniela", "score" => "105", "time" => "7" ),
array("nome" => "Joana", "score" => "100", "time" => "6" ),
array("nome" => "Diogo", "score" => "275", "time" => "4" ),
array("nome" => "Francisco", "score" => "300", "time" => "9" ),
array("nome" => "Ines", "score" => "650", "time" => "2" ),
array("nome" => "Dionisio", "score" => "101", "score" => "10" ),
array("nome" => "Ricardo", "score" => "200", "score" => "8" ),
array("nome" => "Fabio", "score" => "201", "score" => "11" ),
array("nome" => "Tiago","score" => "50", "score" => "13" ),
array("nome" => "Carolina", "score" => "150", "time" => "5" ), 
array("nome" => "Rui", "score" => "130", "time" => "3" ),
array("nome" => "Luisa", "score" => "1000", "time" => "1" ),

);


 usort($Turma, function($a,$b){
    return $b["score"] - $a["score"];


});

 foreach($Turma as $key => $value) {
    
    $position = $key + 1;
    
    echo "{$position}: {$value['nome']} : {$value['score']} <br>";

}






echo "<br>";
echo "WINNERS!! <br> ";

foreach($Turma as $key => $value) {
    
    $position = $key + 1;

    if ($position < 4) {
        echo "{$position}: {$value['nome']} : {$value['score']} <br>";
    }

    
}

Now I’m asked to sort the students in case there’s a score draw the fastest wins, guy has a tie in time and score we have to sort alphabetically.

  • Good afternoon. Post the code instead of posting the image. Click edit.

  • To compare strings use strcmp() or strcasecmp. The difference between them is that in the first the comparison does not distinguish between upper and lower case

  • What is the used version of PHP?

1 answer

1

First detail is that your arrayis with some errors. You have doubled the key score here:

array("nome" => "Ricardo", "score" => "200", "score" => "8" ),

Second detail is that, to make this comparison, as was said, you can use the function strcmp or strcasecmp, but I preferred to use a new approach, introduced in newer versions of php: Spaceship Operator.

usort($Turma, function ($a, $b) {

    $resultado = $b['score'] <=> $a['score'];

    return $resultado === 0 ? $b['time'] <=> $b['time'] : $resultado;
});

The operator <=> will compare the values, can return -1, 0 or 1. When he returns 0, is because the values are equal. So I used a ternary condition so that when it equals 0, the comparison if given by the column time.

Corrected version of your array

$Turma = array(

array("nome" => "Diogo", "score" => "100", "time" => "6" ),
array("nome" => "Joao","score" => "500", "time" => "3" ),
array("nome" => "Miguel", "score" => "125", "time" => "8" ),
array("nome" => "Daniela", "score" => "105", "time" => "7" ),
array("nome" => "Joana", "score" => "100", "time" => "6" ),
array("nome" => "Diogo", "score" => "275", "time" => "4" ),
array("nome" => "Francisco", "score" => "300", "time" => "9" ),
array("nome" => "Ines", "score" => "650", "time" => "2" ),
array("nome" => "Dionisio", "score" => "101", "time" => "10" ),
array("nome" => "Ricardo", "score" => "200", "time" => "8" ),
array("nome" => "Fabio", "score" => "201", "time" => "11" ),
array("nome" => "Tiago","score" => "50", "time" => "13" ),
array("nome" => "Carolina", "score" => "150", "time" => "5" ), 
array("nome" => "Rui", "score" => "130", "time" => "3" ),
array("nome" => "Luisa", "score" => "1000", "time" => "1" ),

);

Test in ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.