Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
sort = rows => {
const { index, direction } = this.state.sortBy;
if (typeof index === "undefined" || typeof direction === "undefined") {
return rows;
}
const less = direction === SortByDirection.desc ? 1 : -1;
const more = -1 * less;
rows.sort((a, b) => {
if (a.cells[index] < b.cells[index]) return less;
if (a.cells[index] > b.cells[index]) return more;
// the values matched, sort by 1st column
if (index !== 0) {
if (a.cells[0] < b.cells[0]) return less;
if (a.cells[0] > b.cells[0]) return more;
}
return 0;
});
return rows;
};
[SET_SORT_BY]: (state, action: SetSortByAction) => {
const { index, direction, isSelectable } = action.payload
const sortedRows = [...state.rows]
// When table is selectable, index must be corrected because of the first row of checkboxes
sortedRows.sort(sorter(isSelectable ? (index - 1) : index))
if (direction === SortByDirection.desc) {
sortedRows.reverse()
}
return { ...state, rows: sortedRows, sortBy: action.payload }
},
[SELECT_ONE]: (state, action: SelectOneAction) => {
sort = rows => {
const { index, direction } = this.state.sortBy;
if (typeof index === "undefined" || typeof direction === "undefined") {
return rows;
}
const less = direction === SortByDirection.desc ? 1 : -1;
const more = -1 * less;
rows.sort((a, b) => {
if (a.cells[index] < b.cells[index]) return less;
if (a.cells[index] > b.cells[index]) return more;
// the values matched, sort by 1st column
if (index !== 0) {
if (a.cells[0] < b.cells[0]) return less;
if (a.cells[0] > b.cells[0]) return more;
}
return 0;
});
return rows;
};