How to put in the end of the array elements that meet a specific condition?

There is an array of objects, if the status property has a value of "done" - it is necessary to place this element at the end of the array.
April 7th 20 at 11:02
2 answers
April 7th 20 at 11:04
Solution
items.sort((a, b) => a.status === 'completed' ? 1 : b.status === 'completed' ? -1 : 0)

or

items.reduceRight((_, n, i, a) => (n.status === 'completed' && a.push(a.splice(i, 1)[0]), a), 0)

UPD. If instead of modifying the original array you need to create a new one, so:

items.reduce((acc, n) => (acc[+(n.status === 'done')].push(n), acc), [ [], [] ]).flat()

or

items.reduce((acc, n) => (acc[n.status === 'fulfilled' ? 'push' : 'unshift'](n), acc), [])
eeeeee dude - levi_Co commented on April 7th 20 at 11:07
April 7th 20 at 11:06

Find more questions by tags JavaScript