posted 06/10/2008 by James

If you're using the function usort inside a cakephp controller and are receiving errors like "Warning: usort() [function.usort]: Invalid comparison function in..." you may be referencing your sorting function incorrectly.

 

Try calling usort this way...

 

class ItemsController extends AppController {

    // if reusable, then you should consider putting this function in app_controller.php

    function item_sorter($a,$b) {

        //... comparison logic

    }

    function doSomething($unsorted_list) {

        usort($unsorted_list, array('ItemsController','item_sorter'));

        //$unsorted_list is now sorted

    }

}

Tags: php / usort / cakephp / code
Share:
facebook myspace digg del.icio.us fark stumbleupon live spurl furl reddit yahoo

COMMENTS (displaying 0 comments)

POST (leave a comment)

Name:
Email:
Message:
Verify:
CAPTCHA Image