R
R
Roman Sarvarov2020-05-31 01:09:28
PHP
Roman Sarvarov, 2020-05-31 01:09:28

usort() function. How to do multiple sorting?

I need to sort the array:
1. First, there are numbers that are divisible by 2 without a remainder (these numbers are also in ascending order).
2. Then all the other numbers come in ascending order.

So far it's been like this:

$arr = [1, 4, 3, 2, 5];

usort(
    $arr, 
    fn($a, $b) => $a % 2 === 0 ? -1 : 1
);
// [4, 2, 5, 3, 1]

// А нужно чтобы было так: [2, 4, 1, 3, 5]


Can this be done somehow within a single usort()?

ps using foreach do not offer to solve this problem

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