English 中文(简体)
在项目组合中与我们接触的习俗——清单结尾时为零
原标题:Custom Sorting with usort in PHP - Numerical sort with 0 at the end of the list
  • 时间:2010-09-28 08:53:34
  •  标签:
  • php
  • sorting

在最后几天里,我试图接手如下:

我有两维的阿雷拉,并试图用我们手提的习俗算法加以分类。

我的问题是,我试图将人数按1 2 3 0顺序排列,因此,零应该是最后的项目。

function customsort($e1, $e2) {
    if ($e1["number"] == $e2["number"]) {
        return $e1["year"] - $e2["year"];
    } elseif ($e1["number"] == 0) {
        return 1;
    } else {
        return $e1["number"] - $e2["number"];
    }
}

我认为,这样做是trick的,但只是名单上的零点的一部分。 我很粗略地指出,清单不是腐败的,而整个阵列的倾销表明我,每个使用入境点都达到零点,但并未打上正确的道路。

Thanks in advance, Johnny

最佳回答

您希望将同样的推理应用到<代码>e2:

function customsort($e1, $e2) {
    if ($e1["number"] == $e2["number"]) {
        return $e1["year"] - $e2["year"];
    } elseif ($e1["number"] == 0) {
        return 1;
    } elseif ($e2["number"] == 0) {
        return -1;
    } else {
        return $e1["number"] - $e2["number"];
    }
}

页: 1 财产具有意义

customsort($foo, $bar) == -1*customsort($bar, $foo)

。 尤其是(你的情况是错误的)

customsort({ number =>3},{ number =>0}); // should give a negative number, because
customsort({ number =>0},{ number =>3}); // gives a positive number
问题回答

暂无回答




相关问题
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 ...

热门标签