0

I have been stuck on this problem for a couple of days now. I just can't seem to wrap my head around what seemingly should be a easy problem.

Here is the array, and I want to sort it by temperature. Descending.

[0] => Array
    (
        [name] => Sandnes
        [latitude] => 58.85244
        [longitude] => 5.73521
        [temperature] => 12.0
    )

[1] => Array
    (
        [name] => Sola
        [latitude] => 58.88854
        [longitude] => 5.65285
        [temperature] => 12.3
    )

[2] => Array
    (
        [name] => Kleppe
        [latitude] => 58.77303
        [longitude] => 5.63329
        [temperature] => 12.1
    )

[3] => Array
    (
        [name] => Hommersåk
        [latitude] => 58.93167
        [longitude] => 5.85111
        [temperature] => 11.3
    )

[4] => Array
    (
        [name] => Ålgård
        [latitude] => 58.76417
        [longitude] => 5.85253
        [temperature] => 10.9
    )

[5] => Array
    (
        [name] => Stavanger
        [latitude] => 58.97005
        [longitude] => 5.73332
        [temperature] => 11.9
    )

[6] => Array
    (
        [name] => Tananger
        [latitude] => 58.93618
        [longitude] => 5.5741
        [temperature] => 12.5
    )

[7] => Array
    (
        [name] => Bryne
        [latitude] => 58.73536
        [longitude] => 5.64766
        [temperature] => 12.4
    )

I have tried multiple different solutions with the many different sorting functions that PHP comes with, but with no luck.

I have seen variants of this question, but their answers always vary. After trying to implement some of the solutions I have seen, and failed, I'm hoping some of you may be of assistance.

Henrik O. Skogmo
  • 2,013
  • 4
  • 19
  • 26
  • 3
    http://stackoverflow.com/questions/14994075/sort-multidimensional-arrays-by-sub-array-key-value – MadScientist Jun 28 '13 at 03:56
  • @ukritic: I will inspect your linked solution, and play around with the suggested answers. But please note that what you linked me to, is still an unsolved problem. – Henrik O. Skogmo Jun 28 '13 at 04:01
  • easy to do with usort() –  Jun 28 '13 at 04:05
  • Check this post http://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php – bansi Jun 28 '13 at 04:06
  • possible duplicate of [Reference: all basic ways to sort arrays in PHP](http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-in-php) – deceze Jun 28 '13 at 12:49

3 Answers3

2

As first suggested by ukratic, this worked great.

function compare_temp($a,$b)
{
    if($a['temperature'] == $b['temperature']) return 0;
    return ($a['temperature'] < $b['temperature']) ? 1 : -1;
}
usort($places, 'compare_temp');
Henrik O. Skogmo
  • 2,013
  • 4
  • 19
  • 26
  • This can be simplified even more by simply subtracting the two temperatures. Note that the return value doesn't have to be -1, 0, or 1. It can be any number less than, equal to, or greater than zero - the exact value doesn't matter. – jcsanyi Jun 28 '13 at 05:36
2

An easy fix with usort():

usort($arr, function ($a, $b){
  return ($b["temperature"]-$a["temperature"]);
});

The PHP manual examples compare in the order $a, $b to give an ascending result. This suggestion compares $b,$a to reverse the sort order.

Here's the PHP reference: usort()

0

Use array_multisort

Try this hope it helps you

$array = $your_array

foreach ($array as $key => $row) {
    $temperature[$key] = $row['temperature'];
 }
 array_multisort($temperature, SORT_DESC, $array);
 echo "<pre>";
 print_r($array);
 echo "</pre>";

See this small CODEPAD DEMO

alwaysLearn
  • 6,882
  • 7
  • 39
  • 67