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 alphabetically except for the first three and last items. For example, like so:
- "Ireland"
- "United Kingdom"
- "United States"
- ...other countries, alphabetically...
- "Everywhere Else"
I have considered using array.sort(), which I understand I can easily use to sort them alphabetically, but I've so far been unsuccessful in figuring out how I can achieve the desired output.
API Response
[
{
"destination_country_id":null,
"primary_cost":"9.50",
"region_id":null,
"destination_country_name":"Everywhere Else",
},
{
"destination_country_id":105,
"primary_cost":"8.00",
"region_id":null,
"destination_country_name":"United Kingdom",
},
{
"destination_country_id":209,
"primary_cost":"9.50",
"region_id":null,
"destination_country_name":"United States",
},
{
"destination_country_id":123,
"primary_cost":"5.00",
"region_id":null,
"destination_country_name":"Ireland",
},
{
"destination_country_id":185,
"primary_cost":"5.00",
"region_id":null,
"destination_country_name":"France",
},
{
"destination_country_id":145,
"primary_cost":"5.00",
"region_id":null,
"destination_country_name":"Spain",
}
]
sortso we can advise you how to correct it?destination_country_namerather than bydestination_country_id?destination_country_namerather thancountry_destination_id. An error on my part. I'm relatively new to JavaScript, I've been teaching myself the syntax and working with small examples, but it's the new way of thinking that I'm having the most trouble with. As such, I didn't write any code yet, mostly thinking about how I could do this. As Felix mentions, I was thinking of removing the desired items, sorting the remaining, and adding them back, but it's the entire process that I have issue with as I can't wrap my head around how I would do that.