Sorting arrays, 2 arrays to compare and sort

say i have 4 players with 4 scores,


$player_1_name = 'A', $player_1_score = 5
$player_2_name = 'B', $player_2_score = 1
$player_3_name = 'C', $player_3_score = 8
$player_4_name = 'D', $player_4_score = 5

I need to sort from min to max scores, so to arrange like this:


$player_2_name = 'B', $player_2_score = 1
$player_1_name = 'A', $player_1_score = 5
$player_4_name = 'D', $player_4_score = 5
$player_3_name = 'C', $player_3_score = 8

$arr_scores = array($player_a_score ,$player_b_score ,$player_c_score ,$player_d_score);

sort($arr_scores);

now scores are sorted 1,5,5,8

the challenge for me is to arrange and find player names after sorting scores.

that i must be able to keep player names in another array, then retrieve them via index keys according to new sorted scores 1,5,5,8…

$arr_name = array($player_1_name,$player_2_name,$player_3_name,$player_4_name);

then make sorting to $arr_name, so i can finally retrieve names AND scores like this,


$arr_name[0] // value is 'B';
$arr_name[1] // value is 'A';
$arr_name[2] // value is 'D';
$arr_name[3] // value is 'C';

$arr_scores[0] // value is '1';
$arr_scores[1] // value is '5';
$arr_scores[2] // value is '5';
$arr_scores[3] // value is '8';

of course, in real project, name is not as simple as A,B,C…

how can i sort scores from min to max and then link back each score to respective player?

There’s nothing stopping sorting by another index if the values are equal. (:

see the tutorial from http://www.informit.com/articles/article.aspx?p=31840&seqNum=6

can you explain how this sort works “function compare” ?

Thanks

I wouldn’t use separate arrays. I would use a multi-dimensional array. eg.
$player[1][‘name’] = ‘A’;
$player[1][‘score’] = 5;

Then I would use array_multisort() to sort by what I wanted to sort by while keeping everything else together.

If you’re stuck with working with the separate arrays, you could create a $temp array from them and multisort that.

That will work fine as long as you don’t care if the names are in order. eg.

<?php
//where A,B,C,D are player names

$arr_s_n = array(array('A', 5), 
		array('B', 1), 
		array('C', 8), 
		array('D', 5), 
		array('E', 1), 
		array('F', 8), 
		array('L', 1), 
		array('K', 8), 
		array('J', 1), 
		array('I', 8), 
		array('H', 1), 
		array('G', 8),
		);

function compare($x, $y)
{
    if ($x[1] == $y[1])
        return 0;
    else
        if ($x[1] < $y[1])
            return - 1;
        else
            return 1;
}


usort($arr_s_n, 'compare');

print_r($arr_s_n); 
?>

it gives

Array
(
    [0] => Array
        (
            [0] => H
            [1] => 1
        )

    [1] => Array
        (
            [0] => J
            [1] => 1
        )

    [2] => Array
        (
            [0] => L
            [1] => 1
        )

    [3] => Array
        (
            [0] => E
            [1] => 1
        )

    [4] => Array
        (
            [0] => B
            [1] => 1
        )

    [5] => Array
        (
            [0] => D
            [1] => 5
        )

    [6] => Array
        (
            [0] => A
            [1] => 5
        )

    [7] => Array
        (
            [0] => G
            [1] => 8
        )

    [8] => Array
        (
            [0] => I
            [1] => 8
        )

    [9] => Array
        (
            [0] => F
            [1] => 8
        )

    [10] => Array
        (
            [0] => C
            [1] => 8
        )

    [11] => Array
        (
            [0] => K
            [1] => 8
        )

)

i couldn’t get through with array_multisort function, but is this the collect one?


//where A,B,C,D are player names

$arr_s_n = array(array('A', 5), array('B', 1), array('C', 8), array('D', 5));

function compare($x, $y)
{
    if ($x[1] == $y[1])
        return 0;
    else
        if ($x[1] < $y[1])
            return - 1;
        else
            return 1;
}


usort($arr_s_n, 'compare');

print_r($arr_s_n);