Force null or empty values to bottom when sorting? #2371
-
Hi all, I've got a column in my table that has some empty or null values. Is there an easy way to force these to always be at the bottom no matter which direction the column is sorted? This is my custom sort method so far: let customSort = (rowA, rowB, columnId) => {
let a = rowA.values[columnId]
let b = rowB.values[columnId]
if (a === '' || a === null) {
if (b === '' || b === null) {
return 0; // Both empty/null
}
return -1; // Sort a to an index lower than b
}
if (b === '' || b === null) {
if (a === '' || a === null) {
return 0; // Both empty/null
}
return 1; // Sort b to an index lower than a
}
if (a > b) return 1;
if (b > a) return -1;
return 0;
} which is referenced in my column config: ...
{
id: 'applications',
Header: 'Application ID',
accessor: a => a.id,
filterable: true,
minWidth: 20,
sortType: customSort,
},
... This seems to work fine when sorting in Descending order, but when I toggle sorting to Ascending, all the blank values are at the top. Is there an easy way to fix this? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 10 replies
-
Here's what I came up with to keep falsy values at the end: const sortTypes = useMemo(
() => ({
alphanumericFalsyLast(rowA, rowB, columnId, desc) {
if (!rowA.values[columnId] && !rowB.values[columnId]) {
return 0;
}
if (!rowA.values[columnId]) {
return desc ? -1 : 1;
}
if (!rowB.values[columnId]) {
return desc ? 1 : -1;
}
return rowA.values[columnId].localeCompare(rowB.values[columnId]);
},
}),
[]
);
const columns = [{
sortType: "alphanumericFalsyLast",
}];
useTable({ columns, sortTypes }, useSortBy); |
Beta Was this translation helpful? Give feedback.
-
I had the same problem. Got it to work by adding useMemo and using same format as @Zertz. Thanks for sharing! It's called out in the docs that any custom sort functions have to be memoized. I think this is key to making it work. |
Beta Was this translation helpful? Give feedback.
-
I had a similar problem today. My solution would translate to something like below for you
A more generic answer would be
Hope this helps someone |
Beta Was this translation helpful? Give feedback.
-
I would like to use a custom sort function and always display rows with undefined value in the respective column at the bottom of the table. They should be at the bottom whether I use desc or asc sorting. The sortingFn does not receive the sort order as param. How can this be achieved with v8 without maintaining the sort state myself? Thanks for your help. |
Beta Was this translation helpful? Give feedback.
-
my solution is to translate "empty" values as last unicode character: /**
* Overriding default sorting function so that empty values are sorted to the end.
* @see: {@link https://tanstack.com/table/v8/docs/api/features/sorting#sorting-functions}
*/
const LAST_UNICODE_CHARACTER = '\u10FFFF';
sortingFns.basic = (rowA, rowB, columnId) => {
const valueA = sanitizeValue(rowA.getValue(columnId));
const valueB = sanitizeValue(rowB.getValue(columnId));
return valueA > valueB ? 1 : valueA < valueB ? -1 : 0;
};
const sanitizeValue = (v: any) =>
v === undefined || v === null || v === '' ? LAST_UNICODE_CHARACTER : v; |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
invertSorting
as a column option already exists. https://tanstack.com/table/latest/docs/guide/sorting#sorting-directionsortUndefined
as a column option already exists. https://tanstack.com/table/latest/docs/guide/sorting#sort-undefined-or-nullish-values