I have a code snippet that sorts a list based on user input, but it fails if the user's input contains spaces. How can I modify the code to handle inputs with spaces without removing the spaces between characters? For example, if the user input is 'Man santra', the list should be sorted according to "Man".
var users = [{
name: 'Devgad Mango'
},
{
name: 'Mantra santra'
},
{
name: 'Prag Mango'
},
{
name: 'Pirate aam Mango'
}, {
name: 'Mango raw'
},
];
function search(input) {
const matches = [];
const remeinder = [];
users.forEach(user => {
user.name.startsWith(input) ?
matches.push(user) :
remeinder.push(user);
});
console.log(matches, remeinder)
// now we sort the matches
matches.sort((a, b) => {
const aa = a.name.toLowerCase();
const bb = b.name.toLowerCase();
if (aa < bb) {
return -1;
}
if (aa > bb) {
return 1;
}
return 0;
});
console.log(matches);
// now we want to push the remeinders to the end of the sorted array.
matches.push(...remeinder);
console.log(matches);
console.log(input);
}
const str = "*-*+&^%$#@!/\Man santra*";
var output = (str.replace(/[\/\\#@^!,+()&$~%.'":;*?`<>{}-]/g, ""));
search(output);