Open Zclhlmgqzc opened 3 years ago
To be clear, you want true
to be a shortcut for the default comparator? that seems like it might be a footgun - you can always provide (a, b) => String(b).localeCompare(a)
if that's what you want.
To be clear, you want
true
to be a shortcut for the default comparator? that seems like it might be a footgun - you can always provide(a, b) => String(b).localeCompare(a)
if that's what you want.
Lexicographical Order
console.log(['ab', 'Bb', 'aB', 'ac'].sort())
// [ 'Bb', 'aB', 'ab', 'ac' ]
console.log(['ab', 'Bb', 'aB', 'ac'].sort((a, b) => a.localeCompare(b)))
// [ 'ab', 'aB', 'ac', 'Bb' ]
console.log(
['ab', 'Bb', 'aB', 'ac'].sort((a, b) => {
if (a > b) {
return 1
} else if (a < b) {
return -1
}
return 0
})
)
// [ 'Bb', 'aB', 'ab', 'ac' ]
native sort increases performance by 10%
Ah, sure, it’d be b - a
if that’s what you want.
I’m not sure why performance matters for the tiny number of keys anyone would be interacting with in a query string?
Ah, sure, it’d be
b - a
if that’s what you want.I’m not sure why performance matters for the tiny number of keys anyone would be interacting with in a query string?
b - a is NaN
lol ah, right, it's the code you provided that would be needed.
regardless tho, what's the use case? When wouldn't you want localeCompare, and when would it be hard to provide the exact comparator you need?
Add support for stringify options sort: boolean