-1

I have an array sorted as follows:

Array ( 
    [0] => Array ( 
                    [x] => 0 
                    [y] => 1 
) 
    [1] => Array ( 
                    [x] => 210 
                    [y] => 1 
 ) 
    [2] => Array ( 
                    [x] => 440 
                    [y] => 1 
 ) 
    [3] => Array ( 
                    [x] => 210 
                    [y] => 2 
 ) 
    [4] => Array ( 
                    [x] => 0 
                    [y] => 2 
 ) 
    )

I have already sorted with the value y using rsort:

rsort($packagesinf, 'my_sortY');
print_r($packagesinf);  

function my_sortY($a, $b)
{
    if ($a->y> $b->y) {
        return -1;
    } else if ($a->y< $b->y) {
        return 1;
    } else {
        return 0; 
    }
}

I would liketo sort for each different Y, the X values in ascending order. In my case, it would be: [0]>[1]>[2]>[4]>[3].

DO you have any idea how to do it taking into account that the priority is first the Y and then the X?

EDIT: So now I have modified the code according to the solutions proposed:

    function my_sortYX($a, $b)
    {
        if ($a->y > $b->y) {
            return 1;
        } else if ($a->y < $b->y) {
            return -1;
        } else {
            if ($a->x > $b->x) {
                return 1;
            } else if ($a->x < $b->x) {
                return -1;
            } else {
                return 0; 
            }
        }
    }

    function sortByY($a, $b) {

        return $a['y'] - $b['y'];
    }

     echo json_encode($packagesinf);
     echo nl2br("\n");
     echo nl2br("\n");

     echo 'my_sortYX';
     echo nl2br("\n");
     usort($packagesinf, 'my_sortYX');
     echo json_encode($packagesinf);
     echo nl2br("\n");
     echo nl2br("\n");


     echo 'sortByY';
     echo nl2br("\n");
     usort($packagesinf, 'sortByY');
     echo json_encode($packagesinf);
     echo nl2br("\n");
     echo nl2br("\n");

Which now is sorting correctly for Y with the function sortByY, but I'm still facing the same problem of sorting also for x...

This is the what I recieve from the echo:

[{"x":0,"y":1},{"x":210,"y":1},{"x":440,"y":1},{"x":210,"y":2},{"x":0,"y":2},{"x":30,"y":3},{"x":420,"y":1}]

my_sortYX
[{"x":30,"y":3},{"x":420,"y":1},{"x":0,"y":2},{"x":210,"y":2},{"x":210,"y":1},{"x":440,"y":1},{"x":0,"y":1}]

sortByY
[{"x":440,"y":1},{"x":0,"y":1},{"x":420,"y":1},{"x":210,"y":1},{"x":210,"y":2},{"x":0,"y":2},{"x":30,"y":3}]

So my_sortYX it is still not working properly, only the sortByY...

Alvaro
  • 1,146
  • 2
  • 17
  • 33
  • 2
    [rsort()](http://www.php.net/manual/en/function.rsort.php) doesn't take a callback argument; perhaps you meant to use [usort()](http://www.php.net/manual/en/function.usort.php) instead – Mark Baker Mar 08 '16 at 11:06
  • 1
    And in answer to your question, replace `} else { return 0;` with `} else if ($a->x > $b->z) { return -1; } else if ($a->x < $b->z) { return -1; } else { return 0; }` – Mark Baker Mar 08 '16 at 11:08
  • Totally right @MarkBaker! – Alvaro Mar 08 '16 at 11:17
  • 1
    @MarkBaker Possibly `$b->z` won't work... – CiaPan Mar 08 '16 at 12:05

2 Answers2

1

Did you try this?

    function my_sortYX($a, $b)
    {
        if ($a->y > $b->y) {
            return -1;
        } else if ($a->y < $b->y) {
            return 1;
        } else {
            if ($a->x > $b->x) {
                return -1;
            } else if ($a->x < $b->x) {
                return 1;
            } else {
                return 0; 
            }
        }
    }

EDIT 1 fixed signs of the return values.
EDIT 2 fixed the component references: replaced $a->x with $a[x], etc.

    function my_sortYX($a, $b)
    {
        if ($a[y] > $b[y]) {
            return 1;
        } else if ($a[y] < $b[y]) {
            return -1;
        } else {
            if ($a[x] > $b[x]) {
                return 1;
            } else if ($a[x] < $b[x]) {
                return -1;
            } else {
                return 0; 
            }
        }
    }

EDIT 2 contd.
Possibly you could drop all else-s, because after return there is no possibility to continue the execution after if-else anyway...

    function my_sortYX($a, $b)
    {
        if ($a[y] > $b[y])
            return 1;
        if ($a[y] < $b[y])
            return -1;

        if ($a[x] > $b[x])
            return 1;
        if ($a[x] < $b[x])
            return -1;

        return 0; 
    }
CiaPan
  • 8,142
  • 2
  • 18
  • 32
  • I've been testing all the functions again and I realized that my_sortY wasn't working neither. I came up with this: `function sortByY($a, $b) { return $a['y'] - $b['y']; }` and now it's correctly sorting by Y, but i still have the same problem sorting by X after – Alvaro Mar 08 '16 at 12:12
  • Possibly you just need to exchange `return 1` with `return -1` – the expression `$a['y'] - $b['y']` value is negative if `y` attribute of `$a` item is less than that of `$b`, and you used `>`, greater-than operator. I change my version of code accordingly. – CiaPan Mar 08 '16 at 13:07
  • Thanks @CiaPan, I'm still working on it but I cannot manage to sort it with your function, I updated the question for more details! – Alvaro Mar 08 '16 at 16:02
  • I finally made it, your code was correct, but in my case the it doesn't work the way `$a->x` but it works perfectly when is put `$a[y]`! If you modify the answer I'll put it as correct! :) – Alvaro Mar 08 '16 at 20:42
1

As Mark Baker said, use usort() instead of rsort.

Other than that the following code should work (untested):

usort($packagesinf, 'my_sortY');
print_r($packagesinf);  

function my_sortY($a, $b)
{
    if ($a->y> $b->y) {
        return -1;
    } else if ($a->y< $b->y) {
        return 1;
    } else {
        if ($a->x> $b->x) {
            return -1;
        } else if ($a->x< $b->x) {
            return 1;
        } else {
            return 0; 
        }
    }
}