简体   繁体   English

如何按日期对 ant 表列进行排序?

[英]How to sort ant table column by date?

I would like to sortantd table by a Date column.我想按Date列对antd 表进行排序。

Trying to sort like sorter: (a, b) => new Date(a) - new Date(b)尝试像排序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.当您单击列标题时,这将自动按日期 ASC 到 DESC、DESC 到 ASC 进行排序。 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) : a , b是表记录,所以你需要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)
}

编辑 Q-56623185-SO-按日期排序

{ {

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() }排序器:(a, b) => 新日期(a.date).getTime() - 新日期(b.date).getTime() }

check date format "DD-MM-YYYY" REPLACE FORMAT IN SORTER ACCORDING TO YOURS检查日期格式“DD-MM-YYYY”根据您的排序替换格式

  {title: "Date",
  dataIndex: "Date",
  sorter: {
    compare: (a, b) =>
      moment(a.Date, "DD-MM-YYYY") - moment(b.Date, "DD-MM-YYYY"),
  },}

based on what I have seen and it works for me this is my solution:根据我所见,它对我有用,这是我的解决方案:

  sorter: {
    compare: (a, b) => {
      a = a.created_date.toLowerCase()
      b = b.created_date.toLowerCase()
      return a > b ? -1 : b > a ? 1 : 0
    },
  },

but it would be better if you could do it sorting from backend API instead of just handle it in Front side.但如果你能从后端 API 排序而不是只在前端处理它会更好。

I hope my solution will be useful我希望我的解决方案有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM