开发者

usort returning unpredicable results

开发者 https://www.devze.com 2023-04-11 05:36 出处:网络
I have an array of objects returned from a database. A print_r on the unsorted array looks like this:

I have an array of objects returned from a database. A print_r on the unsorted array looks like this:

Array
(
    [0] => stdClass Object
        (
            [nid] => 53162
            [title] => Kroger 'Giving Hope' Standee
            [path] => node/53162
        )

    [1] => stdClass Object
        (
            [nid] => 64185
            [title] => Kroger 'Giving Hope' Stanchion Sign
            [path] => node/64185
        )

    [2] => stdClass Object
        (
            [nid] => 52190
            [title] => Betty Crocker Kroger 'Giving Hope' Shipper
            [path] => node/52190
        )

    [3] => stdClass Object
        (
            [nid] => 53159
            [title] => Frito-Lay Kroger 'Giving Hope' Packaging
            [path] => node/53159
        )

    [4] => stdClass Object
        (
            [nid] => 53164
            [title] => Nabisco Kroger 'Giving Hope' Violator
            [path] => node/53164
        )

    [5] => stdClass Object
        (
            [nid] => 52607
            [title] => Doritos Kroger 'Giving Hope' Packaging
            [path] => node/52607
        )

    [6] => stdClass Object
        (
            [nid] => 52720
            [title] => Kroger Big K Cola 'Giving Hope' Packaging
            [path] => node/52720
        )

    [7] => stdClass Object
        (
            [nid] => 52729
            [title] => Windex Kroger 'Giving Hope' Packaging
            [path] => node/52729
        )

    [8] => stdClass Object
        (
            [nid] => 52731
            [title] => Ziploc Kroger 'Giving Hope' Packaging
            [path] => node/52731
        )

    [9] => stdClass Object
        (
            [nid] => 53157
            [title] => Stacy's Kroger 'Giving Hope' Cut Cases
            [path] => node/53157
        )

)

I want to sort the array by the nid property, so I used a custom usort function (found here.)

function my_search_sort($a, $b) {
  strcmp($a->nid, $b->nid);
}

I called it like this:

usort($docs, 'my_search_sort');

and then did another print_r. The results look like this:

Array
(
    [0] => stdClass Object
        (
            [nid] => 52720
            [title] => Kroger Big K Cola 'Giving Hope' Packaging
            [path] => node/52720
        )

    [1] => stdClass Object
        (
            [nid] => 52729
            [title] => Windex Kroger 'Giving Hope' Packaging
            [path] => node/52729
        )

    [2] => stdClass Object
        (
            [nid] => 52731
            [title] => Ziploc Kroger 'Giving Hope' Packaging
            [path] => node/52731
      开发者_JAVA技巧  )

    [3] => stdClass Object
        (
            [nid] => 53157
            [title] => Stacy's Kroger 'Giving Hope' Cut Cases
            [path] => node/53157
        )

    [4] => stdClass Object
        (
            [nid] => 52607
            [title] => Doritos Kroger 'Giving Hope' Packaging
            [path] => node/52607
        )

    [5] => stdClass Object
        (
            [nid] => 53164
            [title] => Nabisco Kroger 'Giving Hope' Violator
            [path] => node/53164
        )

    [6] => stdClass Object
        (
            [nid] => 64185
            [title] => Kroger 'Giving Hope' Stanchion Sign
            [path] => node/64185
        )

    [7] => stdClass Object
        (
            [nid] => 52190
            [title] => Betty Crocker Kroger 'Giving Hope' Shipper
            [path] => node/52190
        )

    [8] => stdClass Object
        (
            [nid] => 53159
            [title] => Frito-Lay Kroger 'Giving Hope' Packaging
            [path] => node/53159
        )

    [9] => stdClass Object
        (
            [nid] => 53162
            [title] => Kroger 'Giving Hope' Standee
            [path] => node/53162
        )

)

It's obviously doing something, but it's not sorting in ascending order according to nid. What do I need to do to get that to work?


Your custom sort function needs to return the value:

function my_search_sort($a, $b) {
  return strcmp($a->nid, $b->nid);
}

EDIT I've updated the code on the other answer you referenced, it was incorrect.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号