首页 > 解决方案 > Antd表,如何按日期排序,包括整个时间戳?

问题描述

我有这个项目

const data: Item[] = [
    {
        key: 1,
        name: 'John Brown',
        date: moment('10-10-2019').format('L'),
        address: 'New York No. 1 Lake Park',
    },
    {
        key: 2,
        name: 'Joe Black',
        date: moment('11-10-2019').format('L'),
        address: 'London No. 1 Lake Park',
    },
    {
        key: 3,
        name: 'Jim Green',
        date: moment('7-10-2019').format('L'),
        address: 'Sidney No. 1 Lake Park',
    },
    {
        key: 4,
        name: 'Jim Red',
        date: moment('8-10-2019 8:00 PM').format('L'),
        // date: moment('8-10-2019 8:00 PM').format('MMMM Do YYYY, h:mm:ss a'),
        address: 'London No. 2 Lake Park',
    },
];

如果我将该数据用于“日期”,并且我使用以下分拣机,它工作正常

sorter: (a, b) => moment(a.date).unix() - moment(b.date).unix()

但我希望数据更复杂一点,像这样

date: moment('8-10-2019 8:00 PM').format('MMMM Do YYYY, h:mm:ss a')

评估为August 10th 2019, 8:00:00 pm

问题是,我无法使用上述排序器对数据进行排序。我怎么能做到?

标签: javascriptreactjstypescriptantd

解决方案


试试这个分拣机:

sorter: (a, b) =>
    new Date(moment(a.date, "MMMM Do YYYY, h:mm:ss a").format("LLL")) -
    new Date(moment(b.date, "MMMM Do YYYY, h:mm:ss a").format("LLL")),

编辑

另请注意,有一个不推荐使用的警告,即您使用的值(您提供的日期)不是可识别的 RFC2822 或 ISO 格式。您可以检查moment文档的有效格式。


推荐阅读