I'm trying to sort an array of objects according to position of a 'restaurant' string in title field
var test = [{
title: 'Roy Restaurant'
},
{
title: 'Restaurant'
},
{
title: 'Roy Mega Restaurant'
}
];
console.log(test.sort(function(a, b) {
return a.title.toLowerCase().indexOf('restaurant') > b.title.toLowerCase().indexOf('restaurant');
}));
so what my expected output is
var test = [
{
title : 'Restaurant'
},
{
title : 'Roy Restaurant'
},
{
title : 'Roy Mega Restaurant'
}
];
so rank them by their indexOf
value. Above, the first item indexOf
value is 0 so it must be the first item, 2nd Roy Restaurant
because the word occurence is less than from the 3rd item.
Any help, ideas please?