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.

4
  • 3
    stackoverflow.com/questions/14994075/… Commented Jun 28, 2013 at 3: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. Commented Jun 28, 2013 at 4:01
  • easy to do with usort() Commented Jun 28, 2013 at 4:05
  • Check this post stackoverflow.com/questions/1597736/… Commented Jun 28, 2013 at 4:06

3 Answers 3

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'); 
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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()

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.