I have an array of objects returning from an API call which I need to sort into a specific format.
I\'m trying to organise the destination_country_id alphab
I think the most efficient way to sort your array is to first find where "Everywhere Else", the "UK", "Ireland", and the "US" are in your array, remove them, and then sort the rest of the array. This is simpler than it sounds
var data = [
{"destination_country_name": "Everywhere Else"},
{"destination_country_name": "United Kingdom"},
{"destination_country_name": "United States"},
{"destination_country_name": "Ireland"},
{"destination_country_name": "France"},
{"destination_country_name": "Spain"} ];
//removed the other elements just to make the example cleaner
var keep = ["Everywhere Else", "Ireland", "United Kingdom", "United States"];
//keep is the elements you want in the front; order them exactly at you want them ordered
var top = [];
//this is our holder array to hold the objects for the strings in keep
for (var i = 0; i < keep.length; i++) {
var index = function () {
for (var j = 0; j < data.length; j++){ //loop through data
if (data[j].destination_country_name == keep[i])
return data[j]; //return the object if it's name matches the one in keep
}
}
if (index > -1){ //if the object is in the array (index != -1)
top.push(data[index]); //push the object to our holder array
data.splice(index, 1); //splice the object out of the original array
}
}
//after this loop, those other objects will have been removed
//sort the rest of that array of objects
data.sort(function (a, b) { //use a callback function to specify which parts of
//the object need to be sorted
//basic sorting/compare algorithm (-1, 0, or 1)
if (a.destination_country_name > b.destination_country_name)
return 1; //if greater
if (a.destination_country_name < b.destination_country_name)
return -1; //if lesser
return 0; //otherwise
})
var sorted = top.concat(data), //combine data to the holder array and assign to sorted
extra = sorted.shift(); //grab the first element ("Everywhere Else") and remove it
sorted.push(extra); //add that element to the end of the array
console.log(sorted);
Alternatively, if you know those four places (EE, UK, US, and Ireland) will always be the first 4 elements in your array, you can do the following:
var data = [
{"destination_country_name": "Everywhere Else"},
{"destination_country_name": "United Kingdom"},
{"destination_country_name": "United States"},
{"destination_country_name": "Ireland"},
{"destination_country_name": "France"},
{"destination_country_name": "Spain"} ];
var top = data.slice(0,4);
data.sort(function (a, b) {
if (a.destination_country_name > b.destination_country_name)
return 1;
if (a.destination_country_name < b.destination_country_name)
return -1;
return 0;
})
var sorted = top.concat(data),
extra = sorted.shift();
sorted = sorted.push(extra); //put "Everywhere Else" at the end of the array
Note how this is much more efficient (and much simpler!) because you don't need to locate those four elements.