0

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"
        }
    ]
}

3 Answers 3

2

Here is an example how you can do this

responseJson.stores.sort(function(a, b) {
    return (a.name.toUpperCase() < b.name.toUpperCase()) ? -1 : (a.name.toUpperCase() > b.name.toUpperCase()) ? 1 : 0;
});

Take a look at my codepen https://codepen.io/ulrichdohou/pen/ERbYRX?editors=0010

Sign up to request clarification or add additional context in comments.

6 Comments

Awesome! Thank you so much that worked like a charm!
Happy coding bro
responseJson.stores.sort(function (a, b) { return (a.name < b.name) ? -1 : (a.name > b.name) ? 1 : 0; });
Yeah thats the same thing in fact. But yours should run faster than mine because you have omited some steps. I will update to make it shorter
No problem. I knew it should have been something simple but I have been trying to figure that one out for awhile now. haha. Thanks for the support man.
|
0

create an empty jsonarray object traverse through responsejson.stores and do the sorting and get the sorted output in empty array later on remove the store data and push the sorted array in responsejson.stores

Comments

0

You can use loadash for this.

const sortedResponse = _.orderBy(responseJson, r => r.stores.name, ['asc'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.