I hope this is not a duplicate question; I have searched to no avail...
I am trying to sort an array of objects returned by a search. I want to sort the results as follows: If the "name" property of an object matches the search query, put it at the top. Next, if the "city" property matches the query, put those next. (I intend to add more criteria later.) So I'm not strictly comparing values, but rather comparing each object $a to the search query $q. The array is indeed being re-indexed, but not in the order I am expecting. What am I missing?
function cmp($a, $b) {
global $q; // search query
$query = strtolower($q);
$name = strtolower($a->getName()); // defined in class
$city = strtolower($a->getCity()); // defined in class
if ($name == $query) {
return -1;
}
elseif ($city == $query) {
return -1;
}
return 0;
}
usort($cinemas, 'cmp'); // $cinemas is an array of objects
$cinemas: Array ( [0] => Cinema Object ( [name:protected] => Sample One [city:protected] => Grapevine ) [1] => Cinema Object ( [name:protected] => Sample Two [city:protected] => Flower Mound ) [2] => Cinema Object ( [name:protected] => Sample Three [city:protected] => TROPHY CLUB ) [3] => Cinema Object ( [name:protected] => Sample Four [city:protected] => Irving ) )$qis a sanitized version of what the user typed in the search input.