You cannot simply subtract titles (strings) from each other and expect a meaningful result. The equivalent for strings is calling the function strcmp
:
function cmp($a, $b) {
return strcmp($a['ForumPost']['title'], $b['ForumPost']['title']);
}
Question
I'm working within the CakePHP framework, and have the following multidimensional array that I will need to sort by values. I'm using usort
, but cannot figure out how to sort by the children of [ForumPost]
.
Here's the array:
Array
(
[0] => Array
(
[ForumPost] => Array
(
[id] => 174
[forum_id] => 81
[user_id] => 39
[title] => A test post
[1] => Array
(
[ForumPost] => Array
(
[id] => 64
[forum_id] => 208
[user_id] => 36
[title] => B test post
... etc
My function and call are:
usort($array, array("ForumSearchesController", "cmp"));
function cmp($a, $b) {
return $a['ForumPost']['title'] - $b['ForumPost']['title'];
}
Solution
You cannot simply subtract titles (strings) from each other and expect a meaningful result. The equivalent for strings is calling the function strcmp
:
function cmp($a, $b) {
return strcmp($a['ForumPost']['title'], $b['ForumPost']['title']);
}