Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Jan Roels
2,308 PointsSort array float values
Hi,
I get the following output from my array:
array (size=381)
0 =>
array (size=2)
'Name' => string 'apple' (length=6)
'Number' => float 0.368844763
1 =>
array (size=2)
'Name' => string 'Blueberry' (length=8)
'Number' => float 5.59E-6
2 =>
array (size=2)
'Name' => string 'Cinnamon' (length=5)
'Number' => float -0.0006947
3 =>
array (size=2)
'Name' => string 'Date' (length=10)
'Number' => float 0.001584453
...
...
...
I want the array to be sorted with the highest 'Number' value first. What's the best way to do this?
2 Answers
Andrew McCormick
17,730 PointsTaken from php.net
function sksort(&$array, $subkey="Number", $sort_ascending=false) {
if (count($array))
$temp_array[key($array)] = array_shift($array);
foreach($array as $key => $val){
$offset = 0;
$found = false;
foreach($temp_array as $tmp_key => $tmp_val)
{
if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey]))
{
$temp_array = array_merge( (array)array_slice($temp_array,0,$offset),
array($key => $val),
array_slice($temp_array,$offset)
);
$found = true;
}
$offset++;
}
if(!$found) $temp_array = array_merge($temp_array, array($key => $val));
}
if ($sort_ascending) $array = array_reverse($temp_array);
else $array = $temp_array;
}

Jan Roels
2,308 PointsAwesome, works like a charm!