Skip to content

sharing sortFns across columns #94

Answered by rwieruch
gofford asked this question in Q&A
Discussion options

You must be logged in to vote

Yes. Usually you have to write sortFns for each column. What may help is defining the columns as an array and then using this array to create the sortFns on the fly:

const columns = [
	{
		title: 'Name',
		sortKey: 'NAME',
		sortFn: (nodes: TableNode[]) =>
			nodes.sort((a, b) => a.name?.localeCompare(b.name)),
		getContent: (node: TableNode) => node.name
	},
	{
		title: 'Description',
		sortKey: 'DESCRIPTION',
		sortFn: (nodes: TableNode[]) =>
			nodes.sort((a, b) => a.description?.localeCompare(b.description)),
		getContent: (node: TableNode) => node.description
	},
 ];

And in your useSort hook:

const sort = useSort(
    data,
    {
      onChange: onSortChange,
    },
    {
      sort…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by gofford
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants