0
Array
(
    [0] => Array
        (
            [post_id] => 70
            [percentage] => 66.666666666667
        )

    [1] => Array
        (
            [post_id] => 72
            [percentage] => 44.444444444444
        )

    [2] => Array
        (
            [post_id] => 74
            [percentage] => 11.111111111111
        )

    [3] => Array
        (
            [post_id] => 82
            [percentage] => 60
        )

)

How to sort this array based on percentage in descending order i tried with this code but it is not working

usort($post_result, array($this, "myfunction"));  
function  myfunction($a, $b)
{
return strcmp($a->percentage, $b->percentage);
}
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
  • possible duplicate of [Sort Multi-dimensional Array by Value](http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value) – Rizier123 Feb 22 '15 at 08:02
  • Thanks @Rizier123 it does , but i need `descending` – Thamaraiselvam Feb 22 '15 at 08:05
  • 1
    Read the php manual for `usort()` and you will figure it out how to change it that it works for you :D If not show us your attempts and where you are stuck and what you don't understand – Rizier123 Feb 22 '15 at 08:07

1 Answers1

2

Try this:

usort($post_result, function($a, $b) {
    if($a['percentage']==$b['percentage']) return 0;
    return $a['percentage'] < $b['percentage']?1:-1;
});
Rizier123
  • 58,877
  • 16
  • 101
  • 156