-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
-
We have cases where we want certain values to always be sorted at the end despite of sort direction.
This can be achieved by passing the column sort direction to a custom sorting function, where we can treat this case.
Eg, have SortingFn
's signature be:
export type SortingFn<TData extends AnyData> = { (rowA: Row<TData>, rowB: Row<TData>, columnId: string, direction: SortDirection): number }
It seems this could help other users too (#3300).
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions
-
👀 1
Replies: 4 comments 8 replies
-
@tannerlinsley Huge fan of your libraries, thanks for all these contributions.
Having undefined values at the bottom of the list regardless of the direction is a reasonable need, yet I believe it is not possible to achieve it easily with the custom sorting functions as of now. Exposing the sort direction seems like a simple yet useful addition that would solve this issue, would you be open to a PR for this?
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2
-
Thanks! This approach of passing a ref to the table model works great in our use case too.
Beta Was this translation helpful? Give feedback.
All reactions
-
A couple of month ago #5486 was merged, which allows undefined
values to always be pulled first or last when sorting, regardless of direction.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
@rpiaggio but this sortUndefined
: last
option does not support null
values unfortunately.
Beta Was this translation helpful? Give feedback.
All reactions
-
I'm doing something like this now:
export const nullAwareNumber: SortingFn<any> = (a, b, columnId) => {
const col = a.getAllCells().find(c => c.column.id === columnId)?.column
const nullsLast = col?.columnDef.meta?.nullsLast ?? false
const x = a.getValue<number | null>(columnId)
const y = b.getValue<number | null>(columnId)
if (x == null && y == null) return 0
if (nullsLast) {
if (x == null) return 1
if (y == null) return -1
} else {
if (x == null) return -1
if (y == null) return 1
}
return (x ?? 0) - (y ?? 0)
}
but I have to "find".the column for each row this way, not sure if this will give a noticeable performance penalty with large datasets...
Beta Was this translation helpful? Give feedback.
All reactions
-
and now I'm doing this everywhere:
columnHelper.accessor(
row => row.someColumnWithNullValues ?? undefined,
{
id: 'someColumn',
header: 'Header',
sortDescFirst: true,
sortUndefined: 'last',
},
),
Beta Was this translation helpful? Give feedback.
All reactions
-
I was reading some old issues like #2371, it seems the direction was part of the function signature. It would be great if we could bring it back.
Beta Was this translation helpful? Give feedback.
All reactions
-
The sortEntry has id and desc however only id is being passed to the sortingFn. It would be very helpful if desc was passed in as well.
However a workaround would be doing something like
`
sortingFn: (obj1: Row, obj2: Row, prop: string) =>
sortUndefinedAtBottom(
customValueFunction(obj1.original),
customValueFunction(obj2.original),
isColumnSortedDesc(obj1, prop)
),
const isColumnSortedDesc = <T,>(column: Row, prop: string) => {
const sort = column._getAllCellsByColumnId()[prop].column.getIsSorted();
return sort === ('desc' as SortDirection);
};
const sortUndefinedAtBottom = (value1: number | undefined, value2: number | undefined, isSortedDesc: boolean) => {
if (value1 === value2) {
return 0;
}
if (value1 === undefined) {
return isSortedDesc ? -1 : 1;
} else if (value2 === undefined) {
return isSortedDesc ? 1 : -1;
}
return value1 - value2;
};
`
Beta Was this translation helpful? Give feedback.
All reactions
-
🎉 1 -
❤️ 1
-
Hi! In case you want to sort columns with range values, for example [241, 567], and for ascending I would like to compare rows by 1st value (241), but for descending by last (567).
It is not possible to write a custom sorting function to achieve that without having sortDirection
.
I would appreciate adding sortDirection
to sortingFn
signature.
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi. I need a specific value to be sorted at the end so really need this feature.
+1 On this. Would really appreciate if sortingFn is added to sortDirection.
Beta Was this translation helpful? Give feedback.