glittershark / reactable

Fast, flexible, and simple data tables in React
glittershark.github.io/reactable
MIT License
1.51k stars 222 forks source link

Sorting by multiple columns #309

Open astraldawn opened 8 years ago

astraldawn commented 8 years ago

Is it possible to have a custom sorting function work on multiple columns?

E.g. Col 1 Col 2
1 A
3 B
2 B
1 B

If I attempt to sort Col 2, the table sorts by Col 2 and then sorts Col 1 in ascending order.

cklab commented 8 years ago

+1, kinda in need of this too as of late.

@glittershark thoughts about providing the row data when calling the sortFunction? I'm using this for the time being, let me know and I can make a PR. https://github.com/cklab/reactable/commit/6b05e0a9a98a24809f852d403c0a9a22cb9834a7

Olliepop commented 7 years ago

@cklab sortFunction should accept row data rather than the cell innerHTML (as it currently does), so we may sort values which have been formatted for our specific usecase without regex within the sort. i.e

{
    column: 'efficiency',
    sortFunction: (rowA, rowB) => {
        return rowA.efficiency > rowB.efficiency ? 1 : -1;
    }
}
isaachinman commented 7 years ago

Is there any update on this? It seems sensible to have fallback sorting, so that if the top-level column sort results in a bunch of rows with equal values, we can have secondary sorting logic for those equal-value rows.

isaachinman commented 7 years ago

@cklab Did you use sortFunction to fallback on a different value comparison if the initial value comparison is identical?

cklab commented 7 years ago

@isaachinman I'm using pr #321 which calls sortFunction like so:

sortFunction(keyA, keyB, rowA, rowB)

So, if we have a sort definition with column: "name" and for the table, we have the data

data=[
  { name: "John", lastName: "Doe" },
  { name: "John", lastName: "Smith" } 
]

sample arguments for sortFunction (with pr #321) would look like:

keyA: "John"
keyB: "John"
rowA: { name: "John", lastName: "Doe" }
rowB: { name: "John", lastName: "Smith" } 

I then use the objects rowA and rowB to perform further comparisons, as needed.