反应时间在特定列上的日期时间排序(列名称交易日期)

时间:2019-03-10 07:17:08

标签: reactjs

我正在使用react表,想对日期时间进行排序,但不能正确地对日期进行升序或降序排序。

列定义代码:使用以下代码对日期时间进行排序。

{Header: 'Transaction Date',accessor: 'transationDate', style: {textAlign: "center"},
                          sortMethod: (a, b) => {
                            var a1 = new Date(a).getTime();
                            var b1 = new Date(b).getTime();
                          if(a1<b1)
                          return 1;
                          else if(a1<b1)
                          return -1;
                          else
                          return 0;
                          }
                        },

降序排序结果:

enter image description here

2 个答案:

答案 0 :(得分:0)

这应该有效!只需用<翻转<。

{Header: 'Transaction Date',accessor: 'transationDate', style: {textAlign: "center"},
                          sortMethod: (a, b) => {
                            var a1 = new Date(a).getTime();
                            var b1 = new Date(b).getTime();
                          if(a1<b1)
                          return 1;
                          else if(a1>b1)
                          return -1;
                          else
                          return 0;
                          }
                        },

答案 1 :(得分:0)

我不知道是否有人仍然为此挣扎,但对我来说,'sortType'代替了'sortMethod'而不是魔术。

{Header: 'Transaction Date',accessor: 'transationDate', style: {textAlign: "center"},
                      sortType: (a, b) => {
                        var a1 = new Date(a).getTime();
                        var b1 = new Date(b).getTime();
                      if(a1<b1)
                      return 1;
                      else if(a1>b1)
                      return -1;
                      else
                      return 0;
                      }
                    },