-1

I am working to make an Angular 9 app on covid-19 cases where I want to sort my objects based on value of nested objects.

Here is my data I want to sort my statewise object alphabetically on basis of value of field s tate:kerela ,state:haryana ;

{
  "history": [
    {
      "day": "2020-03-14",
      "total": {
        "confirmed": 84
      },
      "statewise": [
        {
          "state": "Kerala",
          "confirmed": 19
        },
        {
          "state": "Assam",
          "confirmed": 14
        }
      ]
    },


    {
      "day": "2020-03-15",
      "total": {
        "confirmed": 104
      },
      "statewise": [
        {
          "state": "Kerala",
          "confirmed": 119
        },
        {
          "state": "Assam",
          "confirmed": 114
        }
      ]
    }
  ]
}


so after sorting I want my data like

SOrted Data

{
  "history": [
    {
      "day": "2020-03-14",
      "total": {
        "confirmed": 84
      },
      "statewise": [
        {
          "state": "Assam",
          "confirmed": 14
        },
        {
          "state": "Kerala",
          "confirmed": 19
        }
      ]
    },
    {
      "day": "2020-03-15",
      "total": {
        "confirmed": 104
      },
      "statewise": [
        {
          "state": "Assam",
          "confirmed": 114
        },
        {
          "state": "Kerala",
          "confirmed": 119
        }
      ]
    }
  ]
}

The link to my API data is here API data API data link. I have been trying for two days I have tried this answer but still not getting how should I proceed. Any help will be great.

2 Answers 2

0

The below code will help:

this.postService.getDetails().subscribe(
  response => {
    let sortedData= response.data.history
      .sort(function(a, b) {
        return new Date(b.date) - new Date(a.date);
      }); //Sort based on date

      sortedData.forEach(item=>item.statewise.sort(function(a, b) {
        if (a.state < b.state) {
          return -1;
        }
        if (a.state > b.state) {
          return 1;
        }
        return 0;
      })); //sort state wise in alphabetical order

    console.log(sortedData);
    this.loading = false;
  },
  error => {
    console.log(error);
    this.loading = false;
  }
);

Sample demo:

https://stackblitz.com/edit/angular-rxjs-operators-examples?file=src%2Fapp%2Fstackoverflow%2Fmerge-map-error%2Fmerge-map-error.component.ts

https://angular-rxjs-operators-examples.stackblitz.io/stackoverflow

Look the console for sorted data.

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

4 Comments

The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts(2362) getting error where you have sorted date @Anoop
Is working for me... I tried in stackblitz. Can you open the link - angular-rxjs-operators-examples.stackblitz.io/stackoverflow and check?
Thank you for the update. If found working please click the up button as well. :)
I cant because I dont have reutation of 15+ it was my seocnd question nly
0

You can do it by using sort prototype of array.

let records = {
  "history": [
    {
      "day": "2020-03-14",
      "total": {
        "confirmed": 84
      },
      "statewise": [
        {
          "state": "Kerala",
          "confirmed": 19
        },
        {
          "state": "Assam",
          "confirmed": 14
        }
      ]
    },


    {
      "day": "2020-03-15",
      "total": {
        "confirmed": 104
      },
      "statewise": [
        {
          "state": "Kerala",
          "confirmed": 119
        },
        {
          "state": "Assam",
          "confirmed": 114
        }
      ]
    }
  ]
};

const res = {history: []};

res.history = records.history.map(history => {
	history.statewise.sort((a, b) => a.state < b.state ? -1 : 1);
    return history;
});

console.log(res);
.as-console-wrapper {
    min-height: 100% !important;
}

3 Comments

core.js:6185 ERROR TypeError: Cannot read property 'statewise' of undefined it is giving this error @Sajeeb
Hm WHat could go worng I am doing same in typescript
Code you share your .ts code? At my end its working

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.