My JavaScript challenge involves an array of items like this:
[
'title',
'firstname',
'company',
'[m]usr_phone'
'[m]usr_city'
]
I am looking to implement a custom array sort function that will arrange all non-[m] items at the beginning, followed by [m] items at the end.
I attempted to create a sort function as follows:
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;
}
However, I faced issues making it work correctly. The desired output should be:
[
'company',
'firstname',
'title',
'[m]usr_city'
'[m]usr_phone'
]
Your assistance on this matter is greatly appreciated!