I have this object returned to me through an angular HttpClient Observable.
{
"jsonrpc": "2.0",
"result": [
{
"event": {
"id": "29688772",
"name": "Demoliner/Middelkoop v Behar/Escobar",
"countryCode": "AR",
"timezone": "UTC",
"openDate": "2020-02-08T20:00:00.000Z"
},
"marketCount": 1
},
{
"event": {
"id": "29691591",
"name": "Bemelmans v Pellegrino",
"countryCode": "FR",
"timezone": "UTC",
"openDate": "2020-02-09T09:00:00.000Z"
},
"marketCount": 1
},
{
"event": {
"id": "29690566",
"name": "Diez v Emil Ruusuvuori",
"countryCode": "NL",
"timezone": "UTC",
"openDate": "2020-02-08T13:14:00.000Z"
},
"marketCount": 1
},
{
"event": {
"id": "29690822",
"name": "Koepfer v J Rodionov",
"countryCode": "US",
"timezone": "UTC",
"openDate": "2020-02-08T18:00:00.000Z"
},
"marketCount": 1
},
{
"event": {
"id": "29691586",
"name": "Basic v Vanni",
"countryCode": "FR",
"timezone": "UTC",
"openDate": "2020-02-09T09:00:00.000Z"
},
"marketCount": 1
},
{
"event": {
"id": "29691596",
"name": "P Kotov v Mayot",
"countryCode": "FR",
"timezone": "UTC",
"openDate": "2020-02-09T09:00:00.000Z"
},
"marketCount": 1
}
I want to sort the array by openDate.
I was thinking to do it in the subscription like so
getTennisMatches() {
this.betfairService.getTennisMatches().subscribe((data: any[]) => {
this.matches = data;
this.sortedMatches = this.matches.sort((a, b) => (a.result.event.openDate > b.result.event.openDate) ? 1 : -1);
// OR
this.sortedMatches = _.sortBy(this.matches.result, o => o.result.event.openDate);
});
}
Both sort methods are not working with errors
this.matches.sort is not a function
for vanilla or
Cannot read property 'event' of undefined
for lodash
I think I have to iterate over the events but I'm not sure what I'm doing. I'm using lodash because I find it's syntax easier to understand but I don't have to use it. Thanks