Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Feature Request] Add SortDirection parameter to custom SortingFn #4920

Unanswered
rpiaggio asked this question in Ideas
Discussion options

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!

You must be logged in to vote

Replies: 4 comments 8 replies

Comment options

@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?

You must be logged in to vote
7 replies
Comment options

Thanks! This approach of passing a ref to the table model works great in our use case too.

Comment options

A couple of month ago #5486 was merged, which allows undefined values to always be pulled first or last when sorting, regardless of direction.

Comment options

@rpiaggio but this sortUndefined: last option does not support null values unfortunately.

Comment options

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...

Comment options

and now I'm doing this everywhere:

columnHelper.accessor(
 row => row.someColumnWithNullValues ?? undefined,
 {
 id: 'someColumn',
 header: 'Header',
 sortDescFirst: true,
 sortUndefined: 'last',
 },
 ),
Comment options

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.

You must be logged in to vote
0 replies
Comment options

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;

};
`

You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
1 reply
Comment options

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /