sortby/index.js

21 lines
507 B
JavaScript
Raw Normal View History

2023-09-11 10:31:30 -04:00
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++
}
if (a[property] < b[property]) return order
if (a[property] > b[property]) return -order
}
return 0
}
export { sortby }