Added sorting helper functions for Table Header

This commit is contained in:
apanizo 2018-10-17 17:23:55 +02:00
parent ec630ee39b
commit ee8e168657
1 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,30 @@
// @flow
const desc = (a, b, orderBy) => {
if (b[orderBy] < a[orderBy]) {
return -1
}
if (b[orderBy] > a[orderBy]) {
return 1
}
return 0
}
export const stableSort = (array: any, cmp: any) => {
const stabilizedThis = array.map((el, index) => [el, index])
stabilizedThis.sort((a, b) => {
const order = cmp(a[0], b[0])
if (order !== 0) {
return order
}
return a[1] - b[1]
})
return stabilizedThis.map(el => el[0])
}
export type Order = 'asc' | 'desc'
export const getSorting = (order: Order, orderBy: string) =>
(order === 'desc' ? (a: any, b: any) => desc(a, b, orderBy) : (a: any, b: any) => -desc(a, b, orderBy))