Hello! I can't find, how to sort a table with all row properties?
First i tried defaultSortMethod...
defaultSortMethod = (a, b, desc)
But I need all row data to understand how to sort each of them.
I try to explain i need A & B to be objects with row properties, not only cell value.
Or how to implement the logic other way?
I have some "added" objects, that must be on top of table regardless selected sorting method.
defaultSortMethod = (_a: string, _b: string, desc: boolean): -1 | 0 | 1 => {
const { added } = this.props.history.location.state; // Added objects
const aIsAdded = added ? added.indexOf(_a) > -1 : false;
const bIsAdded = added ? added.indexOf(_b) > -1 : false;
let a = isNil(_a) ? '' : _a;
let b = isNil(_b) ? '' : _b;
a = typeof a === 'string' ? a.toLowerCase() : a;
b = typeof b === 'string' ? b.toLowerCase() : b;
if (!aIsAdded && bIsAdded) {
return desc ? -1 : 1; // Any way on top
}
if (aIsAdded && !bIsAdded) {
return desc ? 1 : -1; // Any way on top
}
if (a > b) {
return 1;
}
if (a < b) {
return -1;
}
return 0;
}
But this function use flat cell values. So if i sort not ID column (not from added array), this logic stops working.
Hope the question is understandable )
I think you may be able to do the following. I don't know if this is the only or best way. I'm just learning to use this component.
For each column:
accessor function: row => row.id (e.g. the name of the column).sortMethod (the values will now be rows, so you can compare, e.g. a.someColName).Cell function: row => row.someColName (or whatever).Thanks a lot, murphyke! Your advice worked perfectly.
Most helpful comment
I think you may be able to do the following. I don't know if this is the only or best way. I'm just learning to use this component.
For each column:
accessorfunction:row => row.id(e.g. the name of the column).sortMethod(the values will now be rows, so you can compare, e.g.a.someColName).Cellfunction:row => row.someColName(or whatever).