bart

Mixing collections

Posted 2 years ago by bart

Hey guys,

I have a collection of items that have two different priorities. I want to sort the items inside of this collection using the priority value but not simply sorting in ascending order. I want to take 3 of priority 1 and then 1 of priority 2, 3 of priority 1 and 1 of priority 2 and so far.

I expect the following result:

Before:

[
    'name' => 'John',
    'priority' => 2,
],
[
    'name' => 'Jack',
    'priority' => 1,
],
[
    'name' => 'Jane',
    'priority' => 1,
],
[
    'name' => 'Leo',
    'priority' => 1,
],
[
    'name' => 'Mike',
    'priority' => 1,
],
[
    'name' => 'Sarah',
    'priority' => 2,
],
[
    'name' => 'Jody',
    'priority' => 1,
],
[
    'name' => 'Lena',
    'priority' => 1,
],

After:

[
    'name' => 'Jack',
    'priority' => 1,
],
[
    'name' => 'Jane',
    'priority' => 1,
],
[
    'name' => 'Leo',
    'priority' => 1,
],
[
    'name' => 'John',
    'priority' => 2,
],
[
    'name' => 'Mike',
    'priority' => 1,
],
[
    'name' => 'Jody',
    'priority' => 1,
],
[
    'name' => 'Lena',
    'priority' => 1,
],
[
    'name' => 'Sarah',
    'priority' => 2,
],

Do you have any idea how to achieve this? Thank you very much!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.