0

How to sort an array of objects which hold again an array of objects and I want to sort it by their last timestamp.

 var weather = [{
    city: 'New York',
    status: 1,
    response: [{
        name: 'Example', lastTimestamp: '2017-12-19T12:43:14.000Z',
        name: 'Example2', lastTimestamp: '2017-12-19T12:42:14.000Z'
    }]
  },
  {
    city: 'Chicago',
    status: 1,
    response: [{
        name: 'Example', lastTimestamp: '2018-05-10T09:00:00.000Z',
        name: 'Example2', lastTimestamp: '2018-05-10T09:04:00.000Z'
    }]
  }
]

In return I want the sorted object like this

 var weather = [
  {
    city: 'Chicago',
    status: 1,
    response: [{
        name: 'Example', lastTimestamp: '2018-05-10T09:00:00.000Z',
        name: 'Example2', lastTimestamp: '2018-05-10T09:04:00.000Z'
    }]
  },
  {
    city: 'New York',
    status: 1,
    response: [{
        name: 'Example', lastTimestamp: '2017-12-19T12:43:14.000Z',
        name: 'Example2', lastTimestamp: '2017-12-19T12:42:14.000Z'
    }]
  }
]
LXhelili
  • 980
  • 5
  • 14
  • 1
    Use `array.sort` with a custom function that checks `lastTimestamp` – bamtheboozle Jul 02 '18 at 11:14
  • 2
    Possible duplicate of [Sort array of objects by string property value in JavaScript](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript) – eisbehr Jul 02 '18 at 11:14
  • 1
    it largely depends on how the sort criteria is based if there is more than one record in `response`. Besides, the `response` elements format is invalid, you probably forgot to add the object notation. That said, you should just use the `sort` prototype with the custom callback. – briosheje Jul 02 '18 at 11:15
  • `[name: …` is a syntax error, since JS arrays cannot have named keys, but only numerical keys. – feeela Jul 02 '18 at 11:16

3 Answers3

1

This might work. It sorts by the city name and if they are equal they are sorted by the lastTimestamp.

var weather = [
    {
        city: 'New York', status: 1, response: {name: 'Example', lastTimestamp: '2017-12-19T12:43:14.000Z'}
    },
    {
        city: 'Chicago', status: 1, response: {name: 'Example', lastTimestamp: '2018-05-10T09:00:00.000Z'}
    },
    {
        city: 'New York', status: 1, response: {name: 'Example', lastTimestamp: '2017-12-20T12:43:14.000Z'}
    },
    {
        city: 'Chicago', status: 1, response: {name: 'Example', lastTimestamp: '2018-05-09T09:00:00.000Z'}
    }
];

weather.sort(function(a,b){
    return a.city>b.city ? 1 :
            a.city<b.city ? -1 : new Date(a.response.lastTimestamp)-new Date(b.response.lastTimestamp)
})

console.log(weather);
Vignesh Raja
  • 7,927
  • 1
  • 33
  • 42
0

Use sort method of Array:

weather.sort(function(obj1, obj2){
   return obj1.response.lastTimestamp < obj2.response.lastTimestamp ? 1 : -1;
});
Alex S.
  • 622
  • 4
  • 6
0

Use sort with your own function. It should look like this:

weather.sort( (a,b) => {
    return new Date(b.response.lastTimestamp) - new Date(a.response.lastTimestamp)
});
Adrian Sawicki
  • 590
  • 5
  • 22