I know there are a lot of sort
related issues but I can not find my solution.
I have data like this:
const list = [
{
name: 'one',
next: 'two'
},
{
name: 'three',
next: 'four'
},
{
name: 'four',
next: 'five'
},
{
name: 'two',
next: 'three'
},
]
And I want to classify them according to the next
property.
I don't want to sort by alphabetical order. But by the next property.
If a.next === b.name
then he come first.
I tried this :
list.sort((a, b) => {
if (a.next === b.name) {
return -1
}
})
How can I achieve this ?
Result I want :
list = [
{
name: 'one',
next: 'two'
},
{
name: 'two',
next: 'three'
},
{
name: 'three',
next: 'four'
},
{
name: 'four',
next: 'five'
}
]