最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to sort array of objects based on descending order of date in reactjs? - Stack Overflow

programmeradmin3浏览0评论

I have an array of objects as below and want to sort it in descending order.

Below is the array of objects

[
{
    "attributes": {

    },
    "timestamp": "2019-04-03T21:00:00+00:00",
},
{
    "attributes": {
    },
    "timestamp": "2019-04-03T09:24:27.179190+00:00",
},
{
    "attributes": {
    },
    "timestamp": "2019-04-03T08:54:06.721557+00:00",
},
{
    "attributes": {

    },
    "timestamp": "2019-04-03T04:54:56.227415+00:00",
},
]

What I have tried?

 let sorted_array = this.state.array.sort((a, b) => a.timestamp - 
     b.timestamp);
 this.setState({array: sorted_array});

But this doesnt work. Could you someone help me with this?

I have an array of objects as below and want to sort it in descending order.

Below is the array of objects

[
{
    "attributes": {

    },
    "timestamp": "2019-04-03T21:00:00+00:00",
},
{
    "attributes": {
    },
    "timestamp": "2019-04-03T09:24:27.179190+00:00",
},
{
    "attributes": {
    },
    "timestamp": "2019-04-03T08:54:06.721557+00:00",
},
{
    "attributes": {

    },
    "timestamp": "2019-04-03T04:54:56.227415+00:00",
},
]

What I have tried?

 let sorted_array = this.state.array.sort((a, b) => a.timestamp - 
     b.timestamp);
 this.setState({array: sorted_array});

But this doesnt work. Could you someone help me with this?

Share Improve this question edited Apr 5, 2019 at 17:26 Amit Verma 41.2k21 gold badges97 silver badges118 bronze badges asked Apr 5, 2019 at 15:43 user11232976user11232976
Add a ment  | 

3 Answers 3

Reset to default 4

Since the timestamps are normalized for lexicographical sort, maybe you can use String.localeCompare() on the sort method:

let input = [
  {
    "attributes": {},
    "timestamp": "2019-04-03T21:00:00+00:00",
  },
  {
    "attributes": {},
    "timestamp": "2019-04-03T09:24:27.179190+00:00",
  },
  {
    "attributes": {},
    "timestamp": "2019-04-03T08:54:06.721557+00:00",
  },
  {
    "attributes": {},
    "timestamp": "2019-04-03T04:54:56.227415+00:00",
  }
];

input.sort((a, b) => b.timestamp.localeCompare(a.timestamp));
console.log(input);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

If you need to sort in ascending order, then use:

input.sort((a, b) => a.timestamp.localeCompare(b.timestamp));

Replace

(a, b) => a.timestamp - b.timestamp

with

(a, b) => a.timestamp.valueOf() - b.timestamp.valueOf()

(That's if the timestamp is indeed a Date object.)

You could create date object of each time stamp and pare those

const data = [
  {
    "attributes": {},
    "timestamp": "2019-04-03T21:00:00+00:00",
  },
  {
    "attributes": {},
    "timestamp": "2019-04-03T09:24:27.179190+00:00",
  },
  {
    "attributes": {},
    "timestamp": "2019-04-03T08:54:06.721557+00:00",
  },
  {
    "attributes": {},
    "timestamp": "2019-04-03T04:54:56.227415+00:00",
  },
]

console.log(data.sort((a, b) => new Date(a.timestamp) - new Date(b.timestamp)));

发布评论

评论列表(0)

  1. 暂无评论