Custom Sorting with usort in PHP - Numerical sort with 0 at the end of the list -


Over the last few days I tried to get my head around:

I have 2 -Image array and I try to sort it using custom algorithms using usort ().

My problem is, that I am trying to sort the numbers in the order of 1 2 3 0 so that zero should always be the last item.

  function customsource ($ e1, $ e2) {if ($ e1 ["number"] == $ e2 ["number"]) {$ e1 Return ["year"] - $ E2 ["year"]; } Otherwise ($ E1 ["number"] == 0) {return1; } And {return $ e1 ["number"] - $ e2 ["number"]; }}  

I thought this would do the trick but only a part of the entries with zero has been sorted at the end of the list. I am completely embarrassed, that the list is not corrupt and by dumping the entire array, I find that every entry entry saves a zero where it should be, but they are not resolved correctly.

Thank you e2 :

"post-text" itemprop = "text">

You forgot to apply the same logic:

($ E1 ["number"] == $ e2 ["number"]) {return $ e1 ["year"] - $ e2 {/ p>
  function customsort ($ E1, $ e2) ["year"]; } Otherwise ($ E1 ["number"] == 0) {return1; } Otherwise ($ E2 ["number"] == 0) {return -1; } And {return $ e1 ["number"] - $ e2 ["number"]; }}  

You are comparing the function should to understand this property

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

for every $ foo and $ bar . Specifically (what went wrong in your case)

  customsort ({'number' => 3}, {'number' => gt; 0}); // should be given a negative number because the customsort ({'number' => gt; 0}, {'number' => 3}); // returns a positive number  

Comments