[英]How to sort the table by date if the date is not filled everywhere?
** 不工作:**
我正在使用 react-table,我做錯了什么? 我目前正在使用該插件的第 7 版。
{
Header: 'DATE',
accessor: 'DATE',
sortType: (rowA, rowB) => {
const a = rowA.values.DATE;
const b = rowB.values.DATE;
if (a !== null && b !== null) {
const dateA = getDate(a);
const dateB = getDate(b);
if (dateA === dateB) {
return 0;
}
if (dateB > dateA) {
return 1;
} else {
return -1;
}
} else {
return 0;
}
},
},
當其中一個是null
時,您將返回0
。 您可以首先根據日期是否為 null 進行sort
。 將空值移到末尾。 然后,根據實際日期值對它們進行排序(假設getDate
返回 Date 對象)
sortType: (rowA, rowB) => {
const a = rowA.values.DATE,
b = rowB.values.DATE;
return (a === null) - (b === null)
|| getDate(a) - getDate(b)
})
這是一個片段:
const array = [null, "2/1/2021", "1/1/2021", null, "3/1/2021"] array.sort( (a, b) => (a === null) - (b === null) || new Date(a) - new Date(b) ) console.log(array)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.