English 中文(简体)
恢复无法预测的成果
原标题:usort returning unpredicable results

我有一系列物体从数据库返回。 在未经编辑的阵列上打字机:

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);
}

我这样说:

usort($docs,  my_search_sort );

之后还有另一台打印机。 结果是:

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
        )

    [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
        )

)

它显然做了thing,但它没有按照 n令分类。 我需要做些什么才能做到这一点?

最佳回答

您的习俗职能需要回归价值:

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.

问题回答

暂无回答




相关问题
How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

Can I Nest OrderBy in .NET?

This doesn t seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m) m.Sent).First.Sent) For Each d As Discussion In x ....

sorting elements javascript

I m looking for a way to sort my elements, but it isn t as easy as it sounds. Please let me explain My elements are grouped per 6 elements (thumbnails), each x represents a thumbnail However all ...

热门标签