I have an array object like
const d = [
{ id: 20, text: 'deaf' },
{ id: 30, text: 'acta', },
{ id: 0, text: 'deema1' },
{ id: -1, text: 'deema2' },
]
I want to sort the array by text but want to keep id -1 and 0 always on top, so the result would be
// sorted
[
{ id: -1, text: 'deema2' },
{ id: 0, text: 'deema1' },
{ id: 30, text: 'acta' },
{ id: 20, text: 'deaf' },
]
I tried to sort like d.sort((a, b) => (a.text).localeCompare(b.text)) but not sure how to handle case -1 and 0
const d = [
{ id: 20, text: 'deaf' },
{ id: 30, text: 'acta', },
{ id: 0, text: 'deema1' },
{ id: -1, text: 'deema2' },
];
d.sort((a, b) => (a.text).localeCompare(b.text))
console.log(d);