按Asc和Desc日期排序

时间:2017-09-28 21:35:18

标签: javascript reactjs sorting immutable.js virtual-table

试图通过desc和/或asc对日期给出的不可变列表进行排序,但它并没有真正正常工作,对单词进行排序它工作正常但不是列表中的下列日期。使用降序和升序值进行虚拟化反应。如果有人能告诉我如何最好地解决这个问题会很有帮助。或者如果没有,还有什么其他选择?

import { List } from 'immutable';
import * as React from 'react';
import { SortDirection } from 'react-virtualized';


class TopComp extends React.Component {
  constructor(props) {
    super(props);
    const data = List([
      {
        0: {
          'Date Reported': 'Mar 16, 2015',
        }
      },
      {
        0: {
          'Date Reported': 'Mar 16, 2015',
        }
      },
      {
        0: {
          'Date Reported': 'Mar 02, 2015',
        }
      },
      {
        0: {
          'Date Reported': 'Mar 02, 2015',
        }
      },
      {
        0: {
          'Date Reported': 'Feb 23, 2015',
        }
      },
      {
        0: {
          'Date Reported': 'Feb 23, 2015',
        }
      },
      {
        0: {
          'Date Reported': 'Oct 07, 2014',
        }
      },
      {
        0: {
          'Date Reported': 'May 30, 2014',
        }
      },
    ]);

    this.state = {
      data,
    };
  }


  render() {
    const { data } = this.state;
    let t = null;
    t = data.sortBy(item => item[0]['Date Reported']).update((t) => {
      console.log(t);
      const Direction = SortDirection.DESC;
      return (Direction === SortDirection.DESC ? t.reverse() : t);
    });
    console.log(t.toJS());

    return (
      <div>
        <span>Hey</span>
      </div>
    );
  }
}


export default TopComp;

不清楚为什么有些日期应该如此?

1 个答案:

答案 0 :(得分:1)

您的日期排序不正确,因为最重要的部分(年份)位于字符串的末尾,排序会将它们视为最不重要的,因此您的问题。

如果您可以以不同的方式获取数据(我希望您可以),请向数据添加另一个元素,该元素可以是时间戳或ISO日期字符串,它将正确排序。

const data = List([
  {
    0: {
      'Date Reported': 'Mar 16, 2015',
      'isodate': "2015-03-16T03:30:49.566+0000"
    }
  },
相关问题