I have an array of objects like so:
[
{
required: false,
sort: 1
},
{
required: true,
sort: 2
},
{
required: false,
sort: 3
},
]
I want to sort it by required DESC, sort ASC. How do you do this?
The result I am looking for is:
[
{
required: true,
sort: 2
},
{
required: false,
sort: 1
},
{
required: false,
sort: 3
},
]
Currently tried with this, which doesnt work. It correctly puts required first, but then ignores sort for the required items:
arr.sort((a, b) => {
if (a.required) return -1
return a.sort - b.sort
})