[Vuejs]-Sort the Json data According to timestamp in Vuejs

0👍

new window.Vue({
el: '#containerwrapper',
data() {
return {
bannerData:""
}
},
created() {
axios.get('http://localhost:8080/pi.json')
.then(response => {
response.data.pressInformation.sort(function(a, b){return b['tstamp']-a['tstamp']}));
this.bannerData = response.data;
});
},
})

0👍

You can use the sort method with a callback function.

From your data it seems that you already have the timestamp in seconds. It is easy so sort using that.

For example you can write something like this:

arr.sort(function(a, b) {
  return +a.pressInformation.tstamp - +b.pressInformation.tstamp;
});

If you are writing a-b denotes ascending order, while b-a is descending order.

The extra + signs are used here to convert from string to int.

Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Leave a comment