usort help with sorting numbers
Posted: Thu May 20, 2010 8:16 pm
I have an array that I want to sort by the "weight" key from biggest to smallest. Here is my code that almost works:
Bu what it does is savy I have the number 7.29 and a number 20, it puts 7.29 (as though it's really 72.9) as greater than 20 instead of the other way around. Here is a dump of my array so you can see the results after I run the above code... 7.29 should be at the bottom of the array...
Code: Select all
function cmp_items_array($a, $b) {
return strcmp($b['weight'], $a['weight']);
}
usort($items_array, "cmp_items_array");Code: Select all
Array
(
[0] => Array
(
[weight] => 7.29
)
[1] => Array
(
[weight] => 20
)
[2] => Array
(
[weight] => 20
)
[3] => Array
(
[weight] => 20
)
[4] => Array
(
[weight] => 20
)
[5] => Array
(
[weight] => 10
)
[6] => Array
(
[weight] => 10
)
)