Note that there are some explanatory texts on larger screens.

plurals
  1. POOrdering results in JSON variable by integer
    primarykey
    data
    text
    <p>I am building a jQuery search suggestion script based upon two Google API's. Each API outputs a "relevance" integer (which I am returning next to each item to demonstrate) and I want to be able to order the results by that integer for each item.</p> <p><strong>How can I do this?</strong> I tried making the script output everything into one variable but I couldn't quite work it out.</p> <p>A working demo can be seen here: <a href="http://jsfiddle.net/rEPf3/" rel="nofollow">http://jsfiddle.net/rEPf3/</a></p> <p>My jQuery code is:</p> <pre><code>$(document).ready(function(){ $("#search").keyup(function(){ $.getJSON("http://suggestqueries.google.com/complete/search?q="+$("#search").val()+"&amp;client=chrome&amp;callback=?",function(data){ var suggestion=""; for(var key in data[1]){ if(data[4]["google:suggesttype"][key]=="NAVIGATION"){ suggestion+="&lt;li&gt;&lt;a href='"+data[1][key]+"'&gt;"+data[2][key]+"&lt;/a&gt; &lt;i&gt;("+data[4]["google:suggestrelevance"][key]+")&lt;/i&gt;&lt;/li&gt;"; }else{ suggestion+="&lt;li&gt;"+data[1][key]+" &lt;i&gt;("+data[4]["google:suggestrelevance"][key]+")&lt;/i&gt;&lt;/li&gt;"; } } $("#suggest").html(suggestion); }); $.getJSON("https://www.googleapis.com/freebase/v1/search?query="+$("#search").val()+"&amp;limit=3&amp;encode=html&amp;callback=?",function(data){ var suggestion2=""; for(var key in data.result){ suggestion2+="&lt;li&gt;"+data.result[key].name+" &lt;i&gt;("+data.result[key].score*4+")&lt;/i&gt;&lt;/li&gt;"; } $("#suggest2").html(suggestion2); }); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload