react-bootstrap-table2
react-bootstrap-table2 copied to clipboard
Custom sorting function for default sort
Hi,
I would like to know how does one invoke custom sort function for default sort. My column has alphanumeric values so the the inbuilt sorting does not work. I have made a custom sort function which works when fine passed via the "sortFunc" parameter with the dataField.
However how does one invoke any custom sorting function on the default sort when the table loads for the first time.
As per the documentation below is the sample for defaultSort:
const defaultSorted = [{ dataField: 'name', // if dataField is not match to any column you defined, it will be ignored. order: 'desc' // desc or asc }];
How do I provide my custom sort function above. It only accepts order, which can be "asc" or "desc"
Thanks, Jimmy
Have a look at https://react-bootstrap-table.github.io/react-bootstrap-table2/storybook/index.html?selectedKind=Sort%20Table&selectedStory=Custom%20Sort%20Fuction&full=0&addons=1&stories=1&panelRight=0&addonPanel=storybook%2Factions%2Factions-panel it shows a sort function
const columns = [{
dataField: 'id',
text: 'Product ID',
sort: true,
// here, we implement a custom sort which perform a reverse sorting
sortFunc: (a, b, order, dataField) => {
if (order === 'asc') {
return b - a;
}
return a - b; // desc
}
}
Thanks very much. I actually already tried this one and this one only works when you click on the column header. This does not automatically invoke when the table loads for the first time.
I want my records to be default sorted by a particular column when the table loads for the first time. Right now records appear unordered and with the above code in place, it only sorts when I manually click on the column header.
Sorry for not ready your question carefully enough. Could you provide a working minimal example on JsFiddle (or somewhere else)?