Jump to content

Calling usort from a class function: where does the comparison function go?


jhsachs
 Share

Recommended Posts

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.