tags:

views:

88

answers:

4
+2  Q: 

PHP Sorting

I am trying to sort an associative array which has multiple vales per entry.

For example

[0] => stdClass Object ( [type] => node [sid] => 158 [score] => 0.059600525242489 )
[1] => stdClass Object ( [type] => node [sid] => 247 [score] => 0.059600525242489 )

I want the array sorted by 'score' (highest score is first index)

How would I do this?

+8  A: 

Use the usort function with this comparison function:

function cmpByScore($a, $b) {
    if ($a['score'] == $b['score']) {
        return 0;
    }
    return $a['score'] > $b['score'] ? 1 : -1;
}

usort($array, 'cmpByScore');
Gumbo
well, there will be more then 2 elements, this is just an example
Steven1350
We know that. Take a look at tj111's post to see how to use this. Two elements are compared at a time and passed to this function.
ryeguy
That doesn’t matter. The `cmpByScore` function is just the function used for the comparison of two specific elements in the sorting algorithm.
Gumbo
+3  A: 

You can do this with the usort function. Here's a quick example of how you could do it.

function sortByScore($a, $b) {
  return $a['score'] - $b['score'];  //if a > b it will return positive, a < b will return negative, a == b returns 0
}

usort($array, "sortByScore");
tj111
Whoah, that's some nifty hackery there with the sort function. Is there any downside to that? (Other than this would only work with integers)
ryeguy
The only downside being it only works with numbers, if your comparing strings you need to use Gumbo's method.
tj111
+5  A: 

If you have PHP 5.3, you can use closures to make this a little more dynamic and pretty in a simple way:

function sortby(&$array, $key)
{
  usort($array, function($a, $b) {
    return ($a[$key] - $b[$key]);
  });
}

Also note that using the minus in the sort function, as suggested by both tj111 and me, will horribly break if you're also planning to sort strings. In that case, Gumbo's approach is the fail-safe way.

skrebbel
Doing the same in PHP < 5.3 is not too difficult either, but the elegant way to do it is by instantiating a small class that keeps track of the key the user specified, and then passing a method in that object to usort.
skrebbel
Anon functions are so pretty, reminds me of javascript. I can't wait for our guys to upgrade to 5.3 (which might be forever at their current pace)
tj111
+1  A: 

Sample code pasted here. Check this URL: link text

$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);

In this example, we will order by volume descending, edition ascending.

We have an array of rows, but array_multisort() requires an array of columns, so we use the below code to obtain the columns, then perform the sorting.

// Obtain a list of columns
foreach ($data as $key => $row) {
    $volume[$key]  = $row['volume'];
    $edition[$key] = $row['edition'];
}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);

The dataset is now sorted, and will look like this:

volume | edition
-------+--------
    98 |       2
    86 |       1
    86 |       6
    85 |       6
    67 |       2
    67 |       7
tummy.developer
See http://stackoverflow.com/editing-help
Gumbo