2

I am trying to sort this multi-dimensional array by rating.

Array
(
    [0] => Array
        (
            [id] => 4
            [image] => test-image.gif
            [name] => Mia Panton
            [description] => From Falkirk
Wearing: River Island
            [month] => 04-2012
            [rating] => 0.0000
            [votes] => 0
        )

    [1] => Array
        (
            [id] => 3
            [image] => test-image.gif
            [name] => Sam Jane Raggett
            [description] => From: Falkirk
Fav Shop: Republic
            [month] => 04-2012
            [rating] => 1.0000
            [votes] => 1
        )

    [2] => Array
        (
            [id] => 2
            [image] => test-image.gif
            [name] => Sasha Westbrooke
            [description] => From Falkirk
Wearing: River Island
            [month] => 04-2012
            [rating] => 4.0000
            [votes] => 2
        )

    [3] => Array
        (
            [id] => 1
            [image] => test-image.gif
            [name] => Max Rose-Collins
            [description] => From: London
Fav Shop: blah
            [month] => 04-2012
            [rating] => 3.3333
            [votes] => 6
        )

)

I am using this to do so

function orderByRating($a, $b) {
    return $a['rating'] - $b['rating'];
}
usort($array, 'orderByRating');

But as you can see it doesn't seem to work as 0 is at the top and then 1 then 4 then 3. I'm not sure if it has any thing to do with the fact the numbers are doubles?!

Any help would be appreciated. Thank you

Max Rose-Collins
  • 1,904
  • 5
  • 26
  • 45

3 Answers3

4

Using Google http://www.php.net/manual/en/function.uasort.php#100325 (and slightly corrected)

function sorting($a, $b)
{
    $d = $a['rating'] - $b['rating'];
    return $d < 0 ? -1 : ($d > 0 ? 1 : 0);
} 
James C
  • 14,047
  • 1
  • 34
  • 43
  • 1
    Right; the problem is that the comparison function needs to return an integer. Max's original function would have worked as long as rating was always an integer. – Charles Apr 28 '12 at 17:27
  • Thank you, I missed that comment on the php manual when I was trying to figure it out. – Max Rose-Collins Apr 28 '12 at 17:32
  • I guess you could do this in another way by converting the values to an integer by doing `$a['rating'] * 10000 - $b['rating'] * 10000` too – James C Apr 28 '12 at 17:46
0

Another approach:
posted by Lohoris (http://stackoverflow.com/a/2699110/1363184)

function aasort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}

aasort($test,"rating");
-1

You should not return a-b because when a-b is something less than 0 it will treat equally.

Please look at this code for example.

<?php
$array = array(1.0,2.9,3.5,4.3);
function orderByRating($a, $b) {
    return $a - $b;
}
function orderByRating2($a, $b) {
    if($a>$b)
        return 1;
    if($a<$b)
        return -0;
    return 0;
}
usort($array, 'orderByRating'); // 1,3.5,2.9,4.3
usort($array, 'orderByRating2'); //1,2.9,3.5,4.3

print_r($array);
chalet16
  • 197
  • 2