0

I have an array $scores as per below.

What I am trying to do is sort the array based on the numeric value in a descending order. I've tried defining functions like suggested here and other ways but haven't been able to do it. Any help or guidance is much appreciated.

------------------------------------------------------------updated------------------------------------------------------------

array ( 0 => array ( 0 => 'B.Howell', 1 => 16.8, ), 1 => array ( 0 => 'B.Osweiler', 1 => 14.88, ), 2 => array ( 0 => 'K.Ballage', 1 => 13.7, ), 3 => array ( 0 => 'F.Owusu', 1 => 8.8, ), 4 => array ( 0 => 'I.Ford', 1 => 6.3, ), 5 => array ( 0 => 'B.Radcliff', 1 => 6.4, ), 6 => array ( 0 => 'D.Fales', 1 => 3.96, ), 7 => array ( 0 => 'L.Carroo', 1 => 4.9, ), 8 => array ( 0 => 'R.Scott', 1 => 2.5, ), 9 => array ( 0 => 'M.Lewis', 1 => 2.4, ), 10 => array ( 0 => 'T.Duarte', 1 => 3.2, ), 11 => array ( 0 => 'J.Langford', 1 => 2.8, ), 12 => array ( 0 => 'A.Derby', 1 => 1.1, ), 13 => array ( 0 => 'D.Morgan', 1 => 1.2, ), )

The solutions offered using usort I've tried previously and could not get them to work and still can't. It does tend to sort the array in somewhat of a descending order, however, there still exists outliers, see the following before and after images.

function mySort($a, $b) {
    return $b[1] - $a[1];
}

usort($scores, 'mySort');

BEFORE IMAGE

AFTER IMAGE

Morts81
  • 419
  • 3
  • 13

2 Answers2

1

It is working as expected with usort() for DESCENDING order of numeric values

$b['1'] - $a['1'] its sorts numeric value descending order

$a['1'] - $b['1'] its sorts numeric value ascending order

<?php
$array = array ( array ('B.Osweiler',14.88 ), 
                 array ('D.Fales', 3.96), 
                 array ('B.Radcliff', 6.4 ), 
                 array ('K.Ballage', 13.7 ), 
                 array ('J.Langford', 2.8 ),
                 array ('B.Howell', 16.8 ) );

print "Before Sort". PHP_EOL;;
print_r($array);
usort($array, function($a, $b) {
    return $b['1'] - $a['1']; //see this line carefully
});

print "After Sort". PHP_EOL;
print_r($array);
?>

DEMO: https://3v4l.org/bWdIq

EDIT: I've added the edit as per the new modification on the question that will fix your existing outliers.

<?php
$array = array ( 0 => array ( 0 => 'B.Howell', 1 => 16.8, ), 1 => array ( 0 => 'B.Osweiler', 1 => 14.88, ), 2 => array ( 0 => 'K.Ballage', 1 => 13.7, ), 3 => array ( 0 => 'F.Owusu', 1 => 8.8, ), 4 => array ( 0 => 'I.Ford', 1 => 6.3, ), 5 => array ( 0 => 'B.Radcliff', 1 => 6.4, ), 6 => array ( 0 => 'D.Fales', 1 => 3.96, ), 7 => array ( 0 => 'L.Carroo', 1 => 4.9, ), 8 => array ( 0 => 'R.Scott', 1 => 2.5, ), 9 => array ( 0 => 'M.Lewis', 1 => 2.4, ), 10 => array ( 0 => 'T.Duarte', 1 => 3.2, ), 11 => array ( 0 => 'J.Langford', 1 => 2.8, ), 12 => array ( 0 => 'A.Derby', 1 => 1.1, ), 13 => array ( 0 => 'D.Morgan', 1 => 1.2, ), );


print '<pre>';
print "Before Sort". PHP_EOL;;
print_r($array);
usort($array, function($a, $b) {
    if($a[1]==$b[1]){
        return 0;
    }

    return ($a[1] > $b[1]) ? -1 : 1;
});

print "After Sort". PHP_EOL;
print_r($array);
?>

DEMO: https://3v4l.org/c4UmQ

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • Guess the OP has taken one of our answers and disappreared into the ether :) SO +1 from me – RiggsFolly Sep 02 '18 at 15:13
  • ha ha :) I also think so :) – A l w a y s S u n n y Sep 02 '18 at 15:17
  • Hey guys, sorry, Australian time meant I went to sleep and then worked all day so I didn't go running with solution. In fact this solution I'd tried before based on the link i had in the question and it unfortunately didn't work for me. I've added extra clarification in the question explaining this now. Appreciate your help on this. – Morts81 Sep 03 '18 at 08:58
  • @Morts81 I've added a new demo as per your question modification. See my edit and let me know whether it is working for you or not now? – A l w a y s S u n n y Sep 03 '18 at 16:15
  • Thanks @smart-googler this works perfectly. Very much appreciated, I imagine this has something to do with the extra comma in each part of the array but I'm not sure why. – Morts81 Sep 05 '18 at 08:54
0

You should be able to use usort() like this

$arr = [ ['B.Osweiler', 14.88],
         ['D.Fales', 3.96 ],
         ['B.Radcliff', 6.4 ],
         ['K.Ballage', 13.7 ], 
         ['J.Langford', 2.8 ],
         ['B.Howell', 16.8 ] 
        ];

function mySort($a, $b) {
    return $a[1] - $b[1];
}

usort($arr, 'mySort');
print_r($arr);

RESULTS

Array
(
    [0] => Array
        (
            [0] => J.Langford
            [1] => 2.8
        )

    [1] => Array
        (
            [0] => D.Fales
            [1] => 3.96
        )

    [2] => Array
        (
            [0] => B.Radcliff
            [1] => 6.4
        )

    [3] => Array
        (
            [0] => K.Ballage
            [1] => 13.7
        )

    [4] => Array
        (
            [0] => B.Osweiler
            [1] => 14.88
        )

    [5] => Array
        (
            [0] => B.Howell
            [1] => 16.8
        )

)
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149