I know we can define our custom sort function of array of json objects. But what if the order is neither desc nor asc
. For example lets say my array looks like:
[ {
name: 'u'
},
{
name: 'n'
},
{
name: 'a'
},
{
name: 'n',
}
]
Output should look like:
[ {
name: 'n'
},
{
name: 'n'
},
{
name: 'a'
},
{
name: 'u',
}
]
Where all the names starting with n
are sorted first and then the rest. I have tried the following custom sort function:
_sortByName(a, b){
if (a.name === 'n'){
return 1;
} else if(b.name === 'n'){
return 1;
} else if(a.name < b.name){
return 1;
} else if(a.name > b.name){
return -1;
}
}
But the order returned for objects is wrong. What is going wrong here?