imballinst / react-bs-datatable

Bootstrap datatable without jQuery. Features include: filter, sort, pagination, checkbox, and control customization.
https://imballinst.github.io/react-bs-datatable
MIT License
60 stars 20 forks source link

Sort by string.toLocaleLowerCase #197

Closed joeleaver closed 9 months ago

joeleaver commented 10 months ago

The sorting behavior for strings is case sensitive. I'd like to be able to sort by string.toLocaleLowercase.

sortValueObj requires a number as its return type, so there isn't a convenient way to do this, unless I'm missing something obvious.

imballinst commented 10 months ago

heya @joeleaver, I think it's a good suggestion! There are 3 ways to do it that I propose:

  1. Have sortValueObj to not only be able to return number, but also string, so string | number, or
  2. In sortValueObj, have some kind of key called __global that will apply the modifier to all columns (so we don't have to do (1) for all keys), or
  3. Make sortValueObj not only able to receive a Record<string, Function> but make it also Record<string, Function> | SortValueFunction. This SortValueFunction is something like (key: name) => string | number. So, we can do something like:
sortValueObj: (key, value) => {
  if (key === 'date') return parse(`${value}`, 'MMMM dd, yyyy', new Date()).getTime()
  return value.toLowerCase()
}

What do you think? I'm thinking maybe option 3 is better but the name sortValueObj would be a bit misleading since it can also accept a function. If we go with 3, maybe I'd consider creating another field with exact same type, but the name is sortValueGetter or sortValueProcessor (and deprecate sortValueObj so we can remove it on the next major version).

imballinst commented 10 months ago

Or maybe we can just simply check if the column value type is a string, then make it toLowerCase() there so no external process is required.

https://github.com/imballinst/react-bs-datatable/blob/9e37868601a2d58451797cd8be125b6a250bd596/src/helpers/data.ts#L25-L26

EDIT: nevermind, I think this is not a good idea since it's a breaking change, probably the 3rd option above is the most ideal

imballinst commented 10 months ago

hi @joeleaver, could you try v3.13.1 and see how it goes with your use case? https://codesandbox.io/p/sandbox/distracted-thunder-nx87tq?file=%2Fsrc%2FApp.tsx%3A77%2C40

sortProps={{
  columnValueProcessor: (key, row) => {
    const value = row[key];
    if (key === "name") {
      return value.toLowerCase();
    }

    return value;
  },
}}
joeleaver commented 9 months ago

This seems to work just fine, thanks for implementing it!

imballinst commented 9 months ago

Cool, thanks for the confirmation! I'll close this issue, then. Let me know if you need further help.