2

I did look at usort, but am still a little confused...

Here is what the $myobject object looks like:

Array
(
    [0] => stdClass Object
        (
            [tid] => 13
            [vid] => 4
        )

    [1] => stdClass Object
        (
            [tid] => 10
            [vid] => 4
        )

    [2] => stdClass Object
        (
            [tid] => 34
            [vid] => 4
        )

    [3] => stdClass Object
        (
            [tid] => 9
            [vid] => 4
        )

I saw this:

function cmp( $a, $b )
{ 
  if(  $a->weight ==  $b->weight ){ return 0 ; } 
  return ($a->weight < $b->weight) ? -1 : 1;
} 
usort($myobject,'cmp');

I'm trying to sort according to tid, but, I guess I'm just not sure really if I have to change weight to something? Or will it just work as is? I tried it, but nothing outputted...

3 Answers 3

6

cmp is the a callback function that usort uses to compare complex objects (like yours) to figure out how to sort them. modify cmp for your use (or rename it to whatever you wish)

function cmp( $a, $b )
{ 
  if(  $a->tid ==  $b->tid ){ return 0 ; } 
  return ($a->tid < $b->tid) ? -1 : 1;
} 
usort($myobject,'cmp');

function sort_by_tid( $a, $b )
{ 
  if(  $a->tid ==  $b->tid ){ return 0 ; } 
  return ($a->tid < $b->tid) ? -1 : 1;
} 
usort($myobject,'sort_by_tid');

http://www.php.net/usort

Sign up to request clarification or add additional context in comments.

4 Comments

Ok, thanks for explaining that, but it's still not working... Am I not allowed to assign that to a variable or something? Because when I do $myobject = usort($myobject, 'cmp'), it doesn't output anything at all? I'm assuming I just need one or the other functions from above, and not both, since they both do the same thing?
Also, I did also try renaming the two variables to $myobject1 and $myobject2 = usort... but that didn't work either...
Nevermind! Should have just tried not assigning it before posting... thanks!
usort is one of the few php functions that takes a pointer as an input (very C-ish in style) and returns a boolean. check out the PHP documentation page. try just putting usort($myobject, 'cmp') and then checking what $myobject has.
0

I have been trying to write a comparing function for three hours. It is very easy in fact but I thought I was missing something and wrote it again and again from scratch changing algorithm in many ways testing it with my sample array.

At last I realized that the problem is at internal uasort function. It does not finish comparing with all items. I don't remember the name of used algorithm right now but I myself use an improved version (ow mine) in C++. The algorithm uses a binary-tree like comparison method by dividing the array into as many pairs as required in a recursive call to the sorting function with new indexes (lower, upper limits) each time.

When the remaining slice is of one item, then upper and lower indexes are the same and function thinks that it has finished (handled all items) although the last item was not evaluated. Sorting functions using that algorithm fail when the most-inner block has an odd number. It works fine 2, 4, 8 .... elements, but cant work with 3, 5, 7 etc... The exact condition of failure depends on the elements sort order. Numbers may not always be meaningful.

I solved that problem years ago. I cant solve it by myself for PHP now because I dont have a PHP compiler and I don't have PHP source code either. But if anybody from PHP development team contacts me, I can supply the working copy of that algorithm in C++. The same algorithm is the fastest way of accessing sorted elements.

Comments

0

For get property stdClass Object use operator ->{'name_property'}, eg $a->{'tid'}

function cmp( $a, $b )
{ 
  if(  $a->{'tid'} ==  $b->{'tid'} ){ return 0 ; } 
  return ($a->{'tid'} < $b->{'tid'}) ? -1 : 1;
} 
usort($myobject,'cmp');

function sort_by_tid( $a, $b )
{ 
  if(  $a->{'tid'} ==  $b->{'tid'} ){ return 0 ; } 
  return ($a->{'tid'} < $b->{'tid'}) ? -1 : 1;
} 
usort($myobject,'sort_by_tid');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.