jhsachs Posted January 31, 2011 Share Posted January 31, 2011 I need to call usort from a class function, and I'm puzzled about how to define the comparison function. I've tried to define the comparison function in the same class, but I can't get usort to call it. I found one hint that it will work if I make the comparison function static, but I tried that, and it didn't work for me. If I define the comparison function outside the class, it won't have access to object properties that it needs to operate. The only solution I can think of is to define the comparison function outside the class and put the object properties it needs in globals. Is there a cleaner way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/226187-calling-usort-from-a-class-function-where-does-the-comparison-function-go/ Share on other sites More sharing options...
cyberRobot Posted January 31, 2011 Share Posted January 31, 2011 I've tried to define the comparison function in the same class, but I can't get usort to call it. I found one hint that it will work if I make the comparison function static, but I tried that, and it didn't work for me. What does the comparison function look like in your class? And how did you attempt to call that function? With the function inside a class, I would imagine you would need to call it like any other class method. From within the class you would call it like $this->usortFunction(). Hopefully someone will correct me if I'm wrong. Quote Link to comment https://forums.phpfreaks.com/topic/226187-calling-usort-from-a-class-function-where-does-the-comparison-function-go/#findComment-1167675 Share on other sites More sharing options...
cyberRobot Posted January 31, 2011 Share Posted January 31, 2011 With the function inside a class, I would imagine you would need to call it like any other class method. From within the class you would call it like $this->usortFunction(). Nevermind, I just reviewed the PHP manual for usort and it looks more complicated that I originally thought. I should have looked there first; sorry about that. But it still might be helpful to post the code you tried. Quote Link to comment https://forums.phpfreaks.com/topic/226187-calling-usort-from-a-class-function-where-does-the-comparison-function-go/#findComment-1167676 Share on other sites More sharing options...
jhsachs Posted January 31, 2011 Author Share Posted January 31, 2011 Well, without burying you in detail... The comparison function looks like this: private static function cmp_sort($a,$b) { . . . } The call looks like this: public function sort_function($offset) { . . . usort( $this->result, "cmp_sort" ); } I've tried specifying the comparison function both as "cmp_sort" and as "\$this->cmp_sort". Neither works. $this->result refers to an array whose elements are arrays returned by calls to mysql_fetch_assoc. The comparison function needs to know which field to sort on and whether to sort in ascending or descending order. Quote Link to comment https://forums.phpfreaks.com/topic/226187-calling-usort-from-a-class-function-where-does-the-comparison-function-go/#findComment-1167710 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.