24 lines
628 B
JavaScript
24 lines
628 B
JavaScript
const sortby = (properties) => (a, b) => {
|
|
// if items is {prop1:value1, prop2:value2}
|
|
// then sort(['prop1', 'desc', 'prop2'])
|
|
// will sort by prop1 desc and prop2
|
|
for (var i = 0; i < properties.length; i++) {
|
|
var property = properties[i]
|
|
|
|
let order = -1
|
|
if (i + 1 < properties.length && properties[i + 1] === 'desc') {
|
|
order = 1
|
|
i++
|
|
}
|
|
|
|
const collator = new Intl.Collator('en', { numeric: true, sensitivity: 'base' })
|
|
const compare = collator.compare(a[property], b[property])
|
|
if (compare > 0) return -order
|
|
if (compare < 0) return order
|
|
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
export { sortby } |