首页 > 解决方案 > ag grid react 比较器 - 对具有日期时间和字符串文字的列进行自定义排序

问题描述

我正在尝试对具有日期时间和字符串文字的 ag 网格反应列进行排序。网格 api 数据如下所示:

export const TableData = [ { id: 9890987, createdBy: "john", updatedBy: "john", scriptType: "new", scriptName: "XYZ", scheduleType: "ONCE", nextRunTime: "Never", lastRunTime: "11/10/2009 15:43:17", lastStatus: "Passed", lastError: "" }, { id: 9891287, createdBy: "Ela", updatedBy: "Ela", scriptType: "new2", scriptName: "XYZ", scheduleType: "HOURLY", nextRunTime: "Never", lastRunTime: "11/10/2014 14:42:17", lastStatus: "Passed", lastError: "" }, { id: 6660987, createdBy: "Mandy", updatedBy: "Mandy", scriptType: "new3", scriptName: "UYT", scheduleType: "MONTHLY", nextRunTime: "Never", lastRunTime: "Never", lastStatus: "Passed", lastError: "" } ] 使用 ag-grid 的比较器属性对 lastRunTime 和 nextRunTime 列进行排序,如下所示,不会以正确的升序或降序排序日期时间:

columnDef = [ { headerName: 'Next Run Time', field: 'nextRunTime', width: 150, comparator: (date1, date2)=> new Date(date1) < new Date(date2) ? -1 : 1 }, { headerName: 'Last Run Time', field: 'lastRunTime', width: 150, comparator: (date1, date2)=> new Date(date1) < new Date(date2) ? -1 : 1 } ]

在这种情况下,可以使用什么比较器算法来启用日期时间排序?

标签: sortingag-grid-reactcolumnsortingcustom-sort

解决方案


推荐阅读