Just sort the server results before sending them to autocomplete.
So, before you echo json_encode($return_arr);
use the sort()
function on $return_arr
You can also try something like this:
The logic is to create an array of matches that begin with this term, and then combine them with matches that contain this term, but not start with it.
$(document).ready(function () { var source = ['Adam', 'Benjamin', 'Matt', 'Michael', 'Sam', 'Tim']; $("input").autocomplete({ source: function (request, response) { var term = $.ui.autocomplete.escapeRegex(request.term) , startsWithMatcher = new RegExp("^" + term, "i") , startsWith = $.grep(source, function(value) { return startsWithMatcher.test(value.label || value.value || value); }) , containsMatcher = new RegExp(term, "i") , contains = $.grep(source, function (value) { return $.inArray(value, startsWith) < 0 && containsMatcher.test(value.label || value.value || value); }); response(startsWith.concat(contains)); } }); });
Example: http://jsfiddle.net/zkVrs/
Source: fooobar.com/questions/600380 / ...
source share