react-bootstrap-table2 icon indicating copy to clipboard operation
react-bootstrap-table2 copied to clipboard

Custom sorting function for default sort

Open aieshgit opened this issue 3 years ago • 3 comments

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

aieshgit avatar Feb 27 '22 23:02 aieshgit

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

donkeyDau avatar Mar 04 '22 13:03 donkeyDau

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.

aieshgit avatar Mar 28 '22 01:03 aieshgit

Sorry for not ready your question carefully enough. Could you provide a working minimal example on JsFiddle (or somewhere else)?

donkeyDau avatar Mar 28 '22 06:03 donkeyDau