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'
    }]
  }
]
4
  • 1
    Use array.sort with a custom function that checks lastTimestamp Commented Jul 2, 2018 at 11:14
  • 2
    Possible duplicate of Sort array of objects by string property value in JavaScript Commented Jul 2, 2018 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. Commented Jul 2, 2018 at 11:15
  • [name: … is a syntax error, since JS arrays cannot have named keys, but only numerical keys. Commented Jul 2, 2018 at 11:16

3 Answers 3

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);

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

Comments

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)
});

1 Comment

No need of getTime()
0

Use sort method of Array:

weather.sort(function(obj1, obj2){
   return obj1.response.lastTimestamp < obj2.response.lastTimestamp ? 1 : -1;
});

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.