G
G
Grigory Vasilkov2016-12-06 00:17:06
PHP
Grigory Vasilkov, 2016-12-06 00:17:06

Why is the sort code not working as expected?

Here is the code that sorts a menu in PHP. I know that this can be done directly with the base, but I want to understand how it works.

usort($menu, function ($a, $b) {
    return (0
      || ($a["level"] - $b["level"])
      || ($a["parent_id"] - $b["parent_id"])
      || ($a["id"] - $b["id"])
    );
  });

I expect it to take two elements, compare levels. If the same, compare the parents, if the parents - compare the id.
Everywhere strictly int numbers.
For some reason, it sorts the devils like, that is, there is no such thing that first there is a section, then a subsection, then a subsubsection, then again a section, its subsections, and so on.
it’s just that most likely he wrote nonsense, the sorting itself is done according to a different principle ...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question