javascript - How to sort concatenated array created from 2 AJAX requests by user's search term? -


i have input box user can type search term.

<input type="text" class="searchbox" placeholder="search here"> 

then, have 2 ajax requests behind scenes, "getmovies" , "getgames", return list of movies , games (respectively) based on user's search term. when user types search term input box, autocomplete suggestions box of similar movie , game titles ajax requests.

this working, autocomplete suggestions not ideally sorted. currently, list of matching games shown, , matching list of movies shown underneath game list, regardless of if movie title matches user's search term better of games above it.

so i'm trying figure out sort suggestions solely based on how closely match user's search term, regardless of genre. have 1 array, combine , containing ajax results both getmovies , getgames, , i've attempted sort before displaying autocomplete suggestions (the commented out section in code below), it's causing errors ("cannot read property 'indexof' of undefined", "cannot read property 'length' of undefined").

please let me know i'm doing wrong , how may fixed. thank you.

var results1 = []; var results2 = []; var combine = [];  $(".searchbox").autocomplete({     source: function(request, response) {            $.when(getgames(request), getmovies(request)         ).done(function(){             combine = results2.concat(results1);                                /*              combine.sort(function(a, b){                 return b[1].indexof(request.term) - a[1].indexof(request.term);             });             */              response(combine);                 }         )     } });  function getmovies(request) {     return $.ajax({         'url': 'http://www.omdbapi.com/?s=' +                  request.term +                 '&type=movie&r=json',         'datatype': 'json',         'success': function(data) {             var list = data.search;              results1 = $.map(list, function(v,i){                 return {                     label: v.title + ' (' + v.year + ')',                     value: v.title                 };             })         }     }); }   function getgames(request) {                   return $.ajax({         type: "post",         url: "php/phpscript.php?search=" + request.term,           datatype: "xml",          success: function(xmlresponse) {                                         results2 = $("game", xmlresponse).map(function() {                 return {                     value: $("gametitle", this).text() + ", g " + ($.trim($("releasedate", this).text()) || "(unknown date)")                 };             }).get()                                    }      }); } 

your arrays contain objects based on structure create in each map() . objects have properties label , value.

you looking a[1] , b[1] don't exist...which why undefined

so sort should looking @ properties of objects , comparing actual values not indexof()

combine.sort(function(a, b){     return b.value.tolowercase() > a.value.tolowercase(); }); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -