I received an array of objects from an API call that I need to organize into a specific format.
My goal is to sort the destination_country_id
alphabetically, with the exception of the first three and last items. For example:
- "Ireland"
- "United Kingdom"
- "United States"
- ...other countries, in alphabetical order...
- "Everywhere Else"
I have thought about using array.sort()
, which should help me alphabetize them easily. However, I am struggling to achieve the desired output so far.
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",
}
]