The documention for useSortBy sortType properties says:
sortType: String | Function(rowA: <Row>, rowB: <Row>, columnId: String, desc: Bool)
Used to compare 2 rows of data and order them correctly.
If a function is passed, it must be memoized. The sortType function should return -1 if rowA is larger, and 1 if rowB is larger. react-table will take care of the rest.
String options: basic, datetime, alphanumeric. Defaults to alphanumeric.
The resolved function from the this string/function will be used to sort the this column's data.
If a string is passed, the function with that name located on either the custom sortTypes option or the built-in sorting types object will be used.
If a function is passed, it will be used.
For more information on sort types, see Sorting
but doesn't explain fully how to use it.
So how does one supply a sortType function?
To define custom sort function, you need to compare first value with second value. If first value is greater than the second value, return -1. If first value is less than the second value, return 1 otherwise return 0. The above process will sort the data in descending order.
To create a table in ReactJS, we need to use a package manager (Yarn or npm) to install a react-table and then import the library into our React app by running the following command. import { useTable } from 'react-table'; After the react-table has been installed and imported, we must describe our data and columns.
The arguments for the sortType function are: (rowA, rowB, columnId, desc)
columnId
identifies which column the row is being sorted by, and so allows getting the cell value.
desc
identifies the direction of the sort. Even though desc
is supplied, the sort function should NOT reverse the return values. react table automatically does this.
For example:
sortType: React.useMemo((rowA, rowB, id, desc) => {
if (rowA.values[id] > rowB.values[id]) return 1;
if (rowB.values[id] > rowA.values[id]) return -1;
return 0;
})
example of where to use sortType:
const columns = [{
Header: ...
accessor: ...
sortType: /*sortType func goes here... */
}, ...]
function MyTable(columns, data)
{
const { /*...*/ } = useTable({columns,data})
}
Per your doc citation, sortType is a Column option.
The following options are supported on any
Column
object passed to thecolumns
options inuseTable()
For instance, modify the Quick Start's Define Columns, like so:
const columns = React.useMemo(
() => [
{
Header: 'Column 1',
accessor: 'col1', // accessor is the "key" in the data
},
{
Header: 'Column 2',
accessor: 'col2',
sortType: compareNumericString // custom function
},
],
[]
)
function compareNumericString(rowA, rowB, id, desc) {
let a = Number.parseFloat(rowA.values[id]);
let b = Number.parseFloat(rowB.values[id]);
if (Number.isNaN(a)) { // Blanks and non-numeric strings to bottom
a = desc ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY;
}
if (Number.isNaN(b)) {
b = desc ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY;
}
if (a > b) return 1;
if (a < b) return -1;
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With