Praveen Praveen - 3 months ago 10
jQuery Question

Sort the values in the jQuery Autocomplete

I have an array like

["JOAQUIN", "BERNARDINO", "MODOC","ALASKA","MADERA", "ANDERSON"]


where I'm populating them in a
jQuery UI Autocomplete
. However the order of sorting seems to be weird.

For example:(But I have more no.of records) When I type "a" it returns

JOAQUIN
BERNARDINO
ALASKA
MADERA
ANDERSON


What I'm trying is to get (starting with)

ALASKA
ANDERSON


JSFiddle for my example

Is it possible? Can someone point me in a right direction.

Updates:

enter image description here

Answer
var myarray= ["JOAQUIN", "BERNARDINO", "MODOC","ALASKA","MADERA", "ANDERSON"]
myarray.sort();

Read More

See Demo

we are passing two arguments into source one is request and second is response,

  • request is stands for request object we are making, in our case it is the letter we are typing into textbox.

  • response is function which will return us auto complete selection options.

now inside $.map we are checking typed words with array we have named json.

json.toUpperCase().indexOf(request.term.toUpperCase()) this line convert typed word and array to same case and return it.

and matches would be the final result which has list of item that you have asked.

and response(matches); will send it to autocomplete.