I have this item
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', }, ];
If I use that data for “date” , and I use the following sorter it works fine
sorter: (a, b) => moment(a.date).unix() - moment(b.date).unix()
But I want the data to be a bit more complex, like this
date: moment('8-10-2019 8:00 PM').format('MMMM Do YYYY, h:mm:ss a')
Which evaluates to August 10th 2019, 8:00:00 pm
Problem is, that I can’t sort that data with the above sorter. How could I do it?
Advertisement
Answer
Try this sorter:
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")),
Also take note that there’s a deprecate warning that you are using a value (your provided date) that is not in a recognized RFC2822 or ISO format. You can check a valid format on moment
documentation.