jquery - Alternative way to sort JSON file or at least, a drop-down list using javascript? -
is there alternative way sort json file or @ least, drop-down list other following code?
json.sort(function(a, b){ return a.id - b.id; });
the reason takes long sort json file containing countries in world. here code:
parsejsonfile("json/country.json", function(data) { data.sort(sortcountryname); localstorage.setitem("country", json.stringify(data)); }); // function sorting countries function sortcountryname(a,b){ return a.spanish.tolowercase() > b.spanish.tolowercase() ? 1 : -1; }
**update: here small part of json file:
{ "code" : "please select country", "english" : "-- please select country --", "spanish" : "-- por favor seleccione el país --" }, { "code" : "afg", "english" : "afghanistan", "spanish" : "afganistán" }, { "code" : "alb", "english" : "albania", "spanish" : "albania" }, { "code" : "dza", "english" : "algeria", "spanish" : "argelia" }, { "code" : "asm", "english" : "american samoa", "spanish" : "samoa americana" },
if can modify json, make sorted in first place @undefined said, user downloads sorted array. if can't, precalculate sort field number of calculations reduced:
var header = data.shift(); data.foreach(function(item) { item.sortkey = item.spanish.tolowercase(); }); data.sort(function(a, b) { var aa = a.sortkey; var bb = b.sortkey; if (aa == bb) return 0; return (aa < bb) ? -1 : 1; }); data.unshift(header);
also, make sure 2 seconds sorting delay , not loading delay. there's not can loading delay apart processing data serverside reduce memory footprint (e.g. compressing transport, pruning specified ui language...)
Comments
Post a Comment