Create one collator per run
parent
f4157d9262
commit
9a93035409
37
index.js
37
index.js
|
@ -1,32 +1,35 @@
|
||||||
const sortby = (...properties) => (a, b) => {
|
|
||||||
|
const sortby = (...properties) => {
|
||||||
// 'base' treat accent as there base character
|
// 'base' treat accent as there base character
|
||||||
const collator = new Intl.Collator('en', { numeric: true, sensitivity: 'base' })
|
const collator = new Intl.Collator('en', { numeric: true, sensitivity: 'base' })
|
||||||
|
|
||||||
// if items is {prop1:value1, prop2:value2}
|
// if items is {prop1:value1, prop2:value2}
|
||||||
// then sort(['prop1', 'desc', 'prop2'])
|
// then sort(['prop1', 'desc', 'prop2'])
|
||||||
// will sort by prop1 desc and prop2
|
// will sort by prop1 desc and prop2
|
||||||
for (let i = 0; i < properties.length; i++) {
|
return (a, b) => {
|
||||||
const property = properties[i]
|
for (let i = 0; i < properties.length; i++) {
|
||||||
|
const property = properties[i]
|
||||||
|
|
||||||
let order = -1
|
let order = -1
|
||||||
if (i + 1 < properties.length) {
|
if (i + 1 < properties.length) {
|
||||||
const next = properties[i + 1]
|
const next = properties[i + 1]
|
||||||
|
|
||||||
if (next === 'desc') {
|
if (next === 'desc') {
|
||||||
order = 1
|
order = 1
|
||||||
i++
|
i++
|
||||||
} else if (next === 'asc') {
|
} else if (next === 'asc') {
|
||||||
i++
|
i++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const compare = collator.compare(a[property], b[property])
|
||||||
|
if (compare < 0) return order
|
||||||
|
if (compare > 0) return -order
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const compare = collator.compare(a[property], b[property])
|
return 0
|
||||||
if (compare < 0) return order
|
|
||||||
if (compare > 0) return -order
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { sortby }
|
export { sortby }
|
Loading…
Reference in New Issue