BlogLabs

Using usort() and symfony2

Jan 13, 2012

The php documentation on the usort() function isn't particularly helpful when it comes to using namespaces and how you call function references.  So if you are using Symfony2 and you want to make use of usort(), then you'll need to do something like the following:

namespace MyBundle\PathTo\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class MyController extends Controller { public function indexAction($sport, $round) { // ... // Create $arrayToSort with items you need to sort in it // ... usort($arrayToSort, array("\MyBundle\PathTo\Controller\MyController", "myComparisonFunction")); } public function myComparisonFunction($a, $b) { if ($a['sortDate'] == $b['sortDate']) { return 0; } return ($a['sortDate'] < $b['sortDate']) ? -1 : 1; } }
Evan
Share on LinkedInTweet about this on Twitter

________________________________________________/\\\_______________________/\\\\\\_____/\\\\\\_________________ _______________________________________________\/\\\______________________\////\\\____\////\\\_________________ _____/\\\______________________________________\/\\\_________________________\/\\\_______\/\\\_________________ __/\\\\\\\\\\\__/\\____/\\___/\\_____/\\\\\____\/\\\_________/\\\____/\\\____\/\\\_______\/\\\_____/\\\\\\\\\\_ _\////\\\////__\/\\\__/\\\\_/\\\___/\\\///\\\__\/\\\\\\\\\__\/\\\___\/\\\____\/\\\_______\/\\\____\/\\\//////__ ____\/\\\______\//\\\/\\\\\/\\\___/\\\__\//\\\_\/\\\////\\\_\/\\\___\/\\\____\/\\\_______\/\\\____\/\\\\\\\\\\_ ____\/\\\_/\\___\//\\\\\/\\\\\___\//\\\__/\\\__\/\\\__\/\\\_\/\\\___\/\\\____\/\\\_______\/\\\____\////////\\\_ ____\//\\\\\_____\//\\\\//\\\_____\///\\\\\/___\/\\\\\\\\\__\//\\\\\\\\\___/\\\\\\\\\__/\\\\\\\\\__/\\\\\\\\\\_ _____\/////_______\///__\///________\/////_____\/////////____\/////////___\/////////__\/////////__\//////////__

Please rotate your device