Using List.js v 2.3.1
I am sorting a column in a table. Column has some blank values. My aim is to sort ONLY non-blank values, and the blank values must always come AFTER non-blank values.
Here's the Custom Sort JavaScript function:
function customSort(a, b, options) {
let valA = a.values()[options.valueName];
let valB = b.values()[options.valueName];
let isBlankA = (valA === '');
let isBlankB = (valB === '');
if (isBlankA && isBlankB) {
return 0;
} else if (isBlankA) {
return 1;
} else if (isBlankB) {
return -1;
} else {
if (valA === valB) {
return 0;
}
if (options.order === 'asc') {
return valA < valB ? -1 : 1;
}
else {
return valA < valB ? 1 : -1;
}
return 0;
}
}
And here's how I am calling it:
sortMapList.sort(colName, { order: sortDir, sortFunction: customSort });
When I send sortDir = 'asc' the column gets sorted in 'desc' order! and vice-versa
Even if I remove the following code
if (options.order === 'asc') {
return valA < valB ? -1 : 1;
}
else {
return valA < valB ? 1 : -1;
}
and replace it with a single line (assuming the order is taken care of by list.js)
return valA < valB ? -1 : 1;
nothing changes.
The Blank data in the column is at the top when in 'asc' order and at the bottom when in 'desc' order.