I have an array of items that I want to sort based on the category
property:
const arr = [{
id: 1,
category: 'bar'
}, {
id: 4,
category: 'baz'
},
{
id: 5,
category: 'foo'
}]
What I want is to sort it based on the following order:
- foo
- bar
- baz
How can I do it with an array comparator?
arr.sort(function(a, b) {
return ?
})
Do I need to give each category an order
number and do a.order - b.order
? Or there is another clever way to do it?