I am trying to figure out how to sort the API response alphabetically by store name before storing the results in the component's state apiResults: []
Here is my code that is called when a form is submitted or button is clicked
getStores() {
let userZip = this.state.userInput;
const api = "some api";
return fetch(api + userZip)
.then((response) => response.json())
.then((responseJson) => {
//sort array alphabetically by store name
//Add code below this line
responseJson = sort()???
// Add code above this line
this.setState({
apiResults: responseJson
});
console.log(this.state.apiResults);
})
.catch((error) => {
console.error(error);
});
}
Here is the sample JSON that I am using.
{
"id": "1",
"code": "35203",
"launch_date": "2016-01-29T00:00:00.000-05:00",
"metro_name": "Birmingham",
"stores": [
{
"id": "1",
"name": "Target",
"launch_date": "2018-01-24T00:00:00.000-05:00"
},
{
"id": "2",
"name": "Costco",
"launch_date": "2017-08-14T00:00:00.000-05:00"
},
{
"id": "3",
"name": "Winn Dixie",
"launch_date": "2018-11-29T00:00:00.000-05:00"
},
{
"id": "4",
"name": "Piggly Wiggly",
"launch_date": "2018-04-10T00:00:00.000-05:00"
}
]
}