Is there an alternative way to sort a JSON file or at least, a drop-down list other than this following code?
json.sort(function(a, b){
return a.id - b.id;
});
The reason for this is that it takes so long to sort the JSON file containing all the countries in the world. Here is my code:
parseJSONFile("json/Country.json", function(data) {
data.sort(sortCountryName);
localStorage.setItem("country", JSON.stringify(data));
});
// Function for sorting Countries
function sortCountryName(a,b){
return a.spanish.toLowerCase() > b.spanish.toLowerCase() ? 1 : -1;
}
**Update: Here is a small part of the 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"
},
.spanish.toLowerCase()and storing it as a field indataitems so you don't duplicate the effort (O(N)invocations vs up toO(N^2)invocations, depending on the underlying algorithm and structure)