usort will not take variable

From: Date: Thu, 19 Sep 2002 16:03:12 +0000
Subject: usort will not take variable
Groups: php.general 
Request: Send a blank email to php-general+get-116898@lists.php.net to get a copy of this message
Hello, Do you know the answer to this got'tcha? I want to call usort with a variable function name. Basically, I want usort to sort any field I specify. Currently, I'm stuck declaring a pile of functions and calling usort from if/elseif statements. :-( Current: function compname ($a, $b) { return strcmp ($a["name"], $b["name"]); } function comptitle ($a, $b) { return strcmp ($a["title"], $b["title"]); } function compcity ($a, $b) { return strcmp ($a["city"], $b["city"]); } function compdesc ($a, $b) { return strcmp ($a["description"], $b["description"]); } function compfile ($a, $b) { return strcmp ($a["filename"], $b["filename"]); } if ($sortby == "name") { usort ($result, "compname"); } elseif ($sortby == "title") { usort ($result, "comptitle"); } elseif ($sortby == "city") { usort ($result, "compcity"); } elseif ($sortby == "description") { usort ($result, "compdesc"); } elseif ($sortby == "filename") { usort ($result, "compfile"); } What Works in Theory: function mysort ($a, $b, $c) { return strcmp ($a["$c"], $b["$c"]); } usort ($result, "$mysort, $sortby"); ...but, that chokes. Your help appreciated. :-) Rob ---------------------------------------------------------------------- Robert J. Miller Internet Support Specialist Department of Government Services and Lands P.O. Box 8700, St. John's, NF., A1B-4J6 (709) 729-4520 (phone) (709) 729-4151 (facsimile) (709) 778-8746 (pager) http://www.gov.nf.ca/gsl/ mailto:rmiller@gov.nf.ca ---------------------------------------------------------------------- Simple things should be simple and hard things should be possible. ----------------------------------------------------------------------

« previous php.general (#116898) next »