0

I am having problem trying to sort an array - I want all the IsOpen = 1 to appear at the top?

Example:

Array
(
    [0] => Array
        (
            [Isopen] => 0
        )

    [2] => Array
        (
            [Isopen] => 1
        )

    [3] => Array
        (
            [Isopen] => 0
        )

    [4] => Array
        (
            [Isopen] => 1
        )

Code:

   function cmp($a, $b) {
        if ($a['Isopen'] >= $b['Isopen']) {
            return 0;
        }
    }

    usort($data['rowResult'], "cmp");

I don't understand what does $a and $b mean, I had a look at the PHP documentation - information is not clear.

6 Answers 6

3

$a and $b are the elements to compare:

function cmp($a, $b) {
    if ($a['Isopen'] == $b['Isopen']) {
        return 0;
    }
    return ($a['Isopen'] > $b['Isopen']) ? -1 : 1;
}
Sign up to request clarification or add additional context in comments.

3 Comments

It appear all the $b['Isopen'] = 1 is at the bottom. Should other way around
are you sure you copy that function? check the < on the last return, in case you put >
Changed from < to > fixed the problem
1

The cmp funciton should return 0, -1 (or less than zero) or 1 (or greater than zero).

  • zero if the two values are equal.
  • greater than zero if the value form a is higher.
  • less than zero if the value from b is higher.

for the comparison of the two values you may use php function strcmp

function cmp($a, $b) {
  return strcmp($a['Isopen'], $b['Isopen']);
}

Comments

1
function cmp($a, $b) {
        if ($a['Isopen'] == $b['Isopen']) {
            return 0;
        }
        return ($a['Isopen'] >  $b['Isopen']) ? -1 : 1;
    }

this will makes Isopen = 1 at the end of the array if you want it at the begging make this

 function cmp($a, $b) {
            if ($a['Isopen'] == $b['Isopen']) {
                return 0;
            }
            return ($a['Isopen'] <  $b['Isopen']) ? -1 : 1;
        }

1 Comment

To make Isopen at the top, I have to change it to: return ($a['Isopen'] < $b['Isopen']) ? 1 : -1; seem to work
0

in this example $a[] and $b[] are two arrays that are handed to the function named 'cmp'. it compares the value of 'Isopen' in two different arrays.

check out the documentation on user defined functions http://php.net/manual/en/language.functions.php

and how to properly use usort() http://php.net/manual/en/function.usort.php

Comments

0

The sorting aglorithm is a Bubble Sort.

Essentially, it compares array entities at a time to determining their ordering. To see a step by step example, see http://en.wikipedia.org/wiki/Bubble_sort#Step-by-step_example

2 Comments

Why reinvent the wheel when he can use library function? And why the slow bubble sort of all algorithms?
@Erbureth Who said anything about reinventing the wheel? I simply provided documentation on how the sort is actually performed, i.e., the algorithm.
0

Your function cmp is not correct (see my example of correct use). Besides if you use php 5.3+ you can use anonymous functions like this:

usort($data['rowResult'], function($a, $b)
{
    if ($a['Isopen'] == $b['Isopen'])
    {
        return 0;
    }

    return $a['Isopen'] < $b['Isopen'] ? -1 : 1;
});

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.