Phil Powell wrote:
asort() does not do anything that I would want since I do not want the
key-val association to be retained. I looked into usort() but that
failed because the callback function that usort calls never gets
called...
$functionBundleArray[0] = get_class($this);
$functionBundleArray[1] = 'field_name_sort';
usort($imageArray, $functionBundleArray, $fieldName);
usort takes /two/ parameters
http://www.php.net/usort
function field_name_sort(&$a, &$b, $fieldName) {
return strcmp(strtolower($a->$fieldName),
strtolower($b->$fieldName));
}
the callback function takes /two/ parameters.
Problem is that $a and $b need to be compared by index, and I have
absolutely NO idea how to do that since usort() doesn't allot for
using any index at all.
So you need to pass a /third/ parameter to the callback function?
Use a global variable :-)
something like this:
#v+
<?php
function user_sort($a, $b) {
global $GLOBAL_VARIABLE_USED_FOR_USER_SORT;
// get a better *local* name :-)
$x = $GLOBAL_VARIABLE_USED_FOR_USER_SORT;
if ($a->$x == $b->$x) return 0;
return ($a->$x < $b->$x) ? -1 : 1;
}
function show($a, $title='') {
echo "$title:\n";
foreach ($a as $k=>$v) {
echo "[$k]";
foreach ($v as $kk=>$vv) echo " $kk = $vv;";
echo "\n";
}
echo "\n\n";
}
$a[0]->fullname = 'Zapotec'; $a[0]->age = 30;
$a[1]->fullname = 'Michael'; $a[1]->age = 80;
$a[2]->fullname = 'Abigail'; $a[2]->age = 17;
show($a);
$GLOBAL_VARIABLE_USED_FOR_USER_SORT = 'fullname';
usort($a, 'user_sort');
show($a, 'sorted by fullname');
$GLOBAL_VARIABLE_USED_FOR_USER_SORT = 'age';
usort($a, 'user_sort');
show($a, 'sorted by age');
echo "\n\n";
?>
#v-
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--