-1

I've created a mirrors system were people can vote for links (Work/Broken) The array looks like this

Array
(
    [0] => Array
        (
            [id] => 1
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 10
            [broken] => 5
        )

    [1] => Array
        (
            [id] => 2
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 10
            [broken] => 9
        )

        [2] => Array
        (
            [id] => 3
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 6
            [broken] => 0
        )

        [3] => Array
        (
            [id] => 4
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 0
            [broken] => 0
        )
        [4] => Array
            (
                [id] => 5
                [link] => link.com/file.zip
                [filename] => file.zip
                [good] => 2
                [broken] => 5
            )

What I'm trying to do is sorting the links based on this: the link who have the best score shows on top I want to calculate the score like this: score = Good - BAD

If the score is negative, it would show links with no votes, so the results of this system will be:

Array 2,0,1,3,4

Mickvolds
  • 3
  • 2
  • possible duplicate of [Reference: all basic ways to sort arrays and data in PHP](http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php) – Barbara Laird Aug 16 '14 at 00:09

1 Answers1

0

Use a custom sort: function.usort. The 'compare' function calculates the difference of 'good' and 'broken' counts as a 'score'. It then compares the 'scores' and returns the appropriate 'indicator' of +1, 0 or -1.

Tested: PHP 5.3.18 Demonstration at viper-7

Code:

// sort the array based on the only the difference between the 'good' and 'broken' scores.
// to force descending sort then reverse the indicator returned by the comparison function.
usort($source, function ($e1, $e2) {
                   $e1Score = $e1['good'] - $e1['broken'];
                   $e2Score = $e2['good'] - $e2['broken'];

                   if ($e1Score < $e2Score) { return +1; }
                   if ($e1Score > $e2Score) { return -1; }
                   return 0;
                }
               );

Test data:

$source = Array(
              Array('id' => 1, 'link' => 'link.com/file.zip', 'filename' => 'file.zip', 'good' => 10, 'broken' => 5),
              Array('id' => 2, 'link' => 'link.com/file.zip', 'filename' => 'file.zip', 'good' => 10, 'broken' => 9),
              Array('id' => 3, 'link' => 'link.com/file.zip', 'filename' => 'file.zip', 'good' =>  6, 'broken' => 0),
              Array('id' => 4, 'link' => 'link.com/file.zip', 'filename' => 'file.zip', 'good' =>  0, 'broken' => 0),
              Array('id' => 5, 'link' => 'link.com/file.zip', 'filename' => 'file.zip', 'good' =>  2, 'broken' => 5),
);

Sorted Output:

Array
(
    [0] => Array
        (
            [id] => 3
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 6
            [broken] => 0
        )

    [1] => Array
        (
            [id] => 1
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 10
            [broken] => 5
        )

    [2] => Array
        (
            [id] => 2
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 10
            [broken] => 9
        )

    [3] => Array
        (
            [id] => 4
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 0
            [broken] => 0
        )

    [4] => Array
        (
            [id] => 5
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 2
            [broken] => 5
        )

)
Ryan Vincent
  • 4,483
  • 7
  • 22
  • 31