I am having trouble getting the proper results returned from a sort on a multidimentiaonl array. I am trying to sort the data by a specific column, then sort that new ordered data again by another column. For example, sort a members list by last name, and then by city.
The $members data looks like this [name], [tel], [city], [number] ...
Smith, 123 4567, Vancouver, 18
Jones, 123 5555, Seattle, 18
Adams, 555 1212, Vancouver, 20
James, 333 2345, Portland, 21
Watts, 454 7777, Vancouver, 19
... after sorting, it should look like this
Jones, 123 5555, Seattle, 18
James, 333 2345, Portland, 21
Adams, 555 1212, Vancouver, 20
Smith, 123 4567, Vancouver, 18
Watts, 454 7777, Vancouver, 19
Here is what I have tried.
Sort by name
function cmp_name($a,$b) {
if ($a[last] == $b[last]) return 0;
return ($a[name]<$b[name]) ? -1 : 1;
}
Sort by city
function cmp_city($a,$b) {
if ($a[city] == $b[city]) return 0;
return ($a[city]<$b[city]) ? -1 : 1;
}
Perform the 2 sorts
usort($member,"cmp_name");
usort($member,"cmp_city");
On there own each function works as I woud expect. However, when I call them one after the other, as shown above, the data is indeed sorted by city, BUT the names are not in alphebetical order.
I have also looked at the array_multisort function, but have yet been able to figure that beast out.
Any ideas would be most apreciated.
Thanks,
-Bob