I would like to sort antd table by a Date column.
Trying to sort like sorter: (a, b) => new Date(a) - new Date(b)
What I've been doing so far here and failed to solve it.
I would like to sort antd table by a Date column.
Trying to sort like sorter: (a, b) => new Date(a) - new Date(b)
What I've been doing so far here and failed to solve it.
Try this one. This will automatically sort by date ASC to DESC, DESC to ASC as you click the column header. You need to install moment
imports:
import moment from 'moment';
Sorter:
sorter: (a, b) => moment(a.date).unix() - moment(b.date).unix()
a,b are table records, so you need new Date(a.date) - new Date(b.date):
{
title: 'Date',
dataIndex: 'date',
key: 'date',
sorter: (a, b) => new Date(a.date) - new Date(b.date)
}
{
title: 'Date',
dataIndex: 'date',
key: 'date',
//if you want to compare dates with in hours or minutes or seconds difference
sorter: (a, b) => new Date(a.date).getTime() - new Date(b.date).getTime() }
check date format "DD-MM-YYYY" REPLACE FORMAT IN SORTER ACCORDING TO YOURS
{title: "Date",
dataIndex: "Date",
sorter: {
compare: (a, b) =>
moment(a.Date, "DD-MM-YYYY") - moment(b.Date, "DD-MM-YYYY"),
},}