从材料表customSort函数中获取排序顺序(asc/desc)



我是react js的新手,我试图对日期进行排序…问题是日期有空值,我希望空日期总是出现在最后,而不管顺序('asc'或'desc')....我见过很多堆栈溢出的例子,他们通过检查顺序来使用customsort…无论是asc还是desc....我使用materialtable,我无法获得排序顺序....我如何获得排序顺序,以便实现自定义排序,其中空日期总是出现在最后....不论asc或desc

我试过了customSort: (a, b) =比;{return (b.paymentDate != null) - (a.paymentDate != null) || a.paymentDate - b.paymentDate;}

这类型…在我的专栏....但它只适用于一种方式.....我想拿到订单……所以我可以按照asc或desc

来写

根据您的示例,看起来您的customSort函数的行为类似于Array.sort()的比较函数。

另外,看起来customSort函数得到了字符串"desc"如果顺序是降序,作为第四个参数:

if (columnDef.customSort) {
if (this.orderDirection === "desc") {
result = list.sort((a, b) => columnDef.customSort(b, a, "row", "desc"));
} else {
result = list.sort((a, b) => columnDef.customSort(a, b, "row"));

所以你可以添加第四个参数到你的customSort,并在此基础上处理null值的排序不同;考虑到如果order == 'desc',调用者交换a和b。是什么:

customSort: (a, b, type, order) => {
let desc = order == "desc";

if (a.paymentDate !== null && b.paymentDate !== null)
return a.paymentDate - b.paymentDate;
else if (a.paymentDate !== null)
return desc ? 1 : -1;
else if (b.paymentDate !== null)
return desc ? -1 : 1;
else
return 0;
}

最新更新