-1

Someone please help me out this issue.

This is my array - I want to order by shop_featured and counts.

Array
(
    [0] => stdClass Object
        (
            [shop_id] => 1
            [shop_featured] => 1
            [counts] => 20
        )

    [1] => stdClass Object
        (
            [shop_id] => 484
            [shop_featured] => 1
            [counts] => 9
        )

    [2] => stdClass Object
        (
            [shop_id] => 886
            [shop_featured] => 0
            [counts] => 0
        )

    [3] => stdClass Object
        (
            [shop_id] => 1279
            [shop_featured] => 0
            [counts] => 0
        )

    [4] => stdClass Object
        (
            [shop_id] => 861
            [shop_featured] => 0
            [counts] => 0
        )

    [5] => stdClass Object
        (
            [shop_id] => 242
            [shop_featured] => 0
            [counts] => 0
        )

    [6] => stdClass Object
        (
            [shop_id] => 1187
            [shop_featured] => 0
            [counts] => 0
        )

    [7] => stdClass Object
        (
            [shop_id] => 906
            [shop_featured] => 0
            [counts] => 2
        )

    [8] => stdClass Object
        (
            [shop_id] => 297
            [shop_featured] => 0
            [counts] => 9
        )

    [9] => stdClass Object
        (
            [shop_id] => 838
            [shop_featured] => 0
            [counts] => 9
        )

    [10] => stdClass Object
        (
            [shop_id] => 1181
            [shop_featured] => 0
            [counts] => 2
        )

    [11] => stdClass Object
        (
            [shop_id] => 620
            [shop_featured] => 0
            [counts] => 0
        )

)

I used the below functions for this

usort($value, array('model_shop', 'cmp1'));
usort($value, array('model_shop', 'cmp'));


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

function cmp1($a, $b)
{
    if ($a->counts == $b->counts) {
    return 0;
    }
    return ($a->counts < $b->counts) ? 1 : -1;
}

Advance thanks

Steven Moseley
  • 15,871
  • 4
  • 39
  • 50
Aaru
  • 803
  • 4
  • 13
  • 29
  • What do you mean by *order by shop_featured, and counts*? Do you want to have to differently sorted copies of `$value`? Do you want a single sorted result where equal elements according to `shop_featured` are sorted according to `count`? Why do you pass `array('model_shop', 'cmp1')` as a comparison function? Is `cmp1` a static member function of the `model_shop` class? Then this should be reflected in the source code that you posted. – Oswald Mar 10 '14 at 09:58

1 Answers1

1

Something like this should sort by shop_featured, then counts

usort($value, array('model_shop', 'cmp'));

function cmp($a, $b)
{
    if ($a->shop_featured == $b->shop_featured) {
        if ($a->counts == $b->counts) {
            return 0;
        }
        return ($a->counts < $b->counts) ? 1 : -1;
    }
    return ($a->shop_featured < $b->shop_featured) ? 1 : -1;
}
Steven Moseley
  • 15,871
  • 4
  • 39
  • 50