1

I have the following function:

public static function CompareGroupReportEntries($a, $b)
{
    if ($a->visibility == $b->visibility) {
        return 0;
    } else{
        return $a->visibility < $b->visibility ? 1 : -1;        
    }
}

It works fine and I understand what it does. However I have difficulty in understanding it how it works. It is called on the following line;

usort($reports, "Utilities::CompareGroupReportEntries");

It is called outside of a loop, so how does it manage to sort all the objects in the array? What are the parameters $a and $b for?

Appreciate the help.

1

3 Answers 3

1

YOur answer here: PHP's USORT Callback Function Parameters

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

Comments

0

it compares the elements of the array to determine their position.

so $a and $b are to elements of the list and from the result of the CompareGroupReportEntries-function it can say which value is bigger, so it can move it one up or down

Comments

0

The function usort is a sorting function which takes an array and a callback function. here your function CompareGroupReportEntries is a callback function. PHP doesnt care whether your array elements are nubers or strings. it expects your callback function for the sorting criteria of the values of your array. iIf u are familiar with C then there is a quicksort function which also takes a callback function

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.