Sorting table values with case insensitivity in react-data-sort


(Yamini) #1

Hi,

I am using react-data-sort for table sort and pagination.I am unable to sort that data with case insensitivity.

The table list data in this format
A
B
C
a
b
c
Rather I want the sort to be in this format
A
a
B
b
C
c

Please let me know what changes has to be done

Thanks


(Joshua) #2

How is this related to npm?


(Kat Marchán) #3

This board is for general JS discussion, so this topic is ontopic for it.


(Lars Willighagen) #4

I changed that after he posted the comment


(Kat Marchán) #5

oh you moved it over here. Gotcha. Thanks! Maybe leave a note when you move it in the future to avoid confusion?


(Lars Willighagen) #6

You might be better off asking there, on their issue tracker. There doesn’t seem to be a feature to do that at the moment. Below is a hint to point you to a way this could be done anyway, but no guarantees.

data
  .sort((a, b) => {
    // whatever comparison you want

    // see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
    // for how to use this comparison

    // and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator
    // for an example comparison that allows for alphabetical sorting
    // with more control
  })
  .forEach((object, index) => {
    // Assuming react-data-sort sorts with the default behaviour, which is comparing them as strings,
    // you have to prefix the numbers, as '10' would sort before '2' otherwise.
    let key = index.toString().padStart(data.length.toString().length, '0')
    object.key = key
  })

return (
  <DataSort
    data={data}
    defaultSortBy='key'
  />
)

(system) #7

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.