The correct complete solution:
var data = [ 'pizzeria', 'berpizzo', 'apizzetto', 'pizza' ]; var _sortByTerm = function (data, term) { return data.sort(function (a, b) { return a.indexOf(term) < b.indexOf(term) ? -1 : 1; }); }; var result = _sortByTerm(data, 'piz');
If you want to sort objects, use this function:
var _sortByTerm = function (data, key, term) { return data.sort(function (a, b) { return a[key].indexOf(term) < b[key].indexOf(term) ? -1 : 1; }); };
source share