In javascript, I have an array of items like so:
[
'title',
'firstname',
'company',
'[m]usr_phone'
'[m]usr_city'
]
I would like to sort this using a custom array sort function so that all the non [m] items are sorted at the top, and all the [m] items are sorted and pushed to the bottom (after all the non [m] items)
To achieve this I tried a sort function like this:
function(a, b) {
if (!a.indexOf('[m]') && b.indexOf('[m]') === 0
|| a.indexOf('[m]') && b.indexOf('[m]')) {
return -1;
}
if (a.indexOf('[m]') === 0 && !b.indexOf('[m]')) {
return 1;
}
return 0;
}
But couldn't get it to work properly. I would like the output to be:
[
'company',
'firstname',
'title',
'[m]usr_city'
'[m]usr_phone'
]
Thanks for your help!