const ZERO = 48
const NINE = 57
export function compare(a: string, z: string) {
let aLen = a.length
let zLen = z.length
let minLen = aLen < zLen ? aLen : zLen
for (let i = 0; i < minLen; i++) {
let aCode = a.charCodeAt(i)
let zCode = z.charCodeAt(i)
if (aCode === zCode) continue
if (aCode >= ZERO && aCode <= NINE && zCode >= ZERO && zCode <= NINE) {
let aStart = i
let aEnd = i + 1
let zStart = i
let zEnd = i + 1
aCode = a.charCodeAt(aEnd)
while (aCode >= ZERO && aCode <= NINE) aCode = a.charCodeAt(++aEnd)
zCode = z.charCodeAt(zEnd)
while (zCode >= ZERO && zCode <= NINE) zCode = z.charCodeAt(++zEnd)
let aNumber = a.slice(aStart, aEnd)
let zNumber = z.slice(zStart, zEnd)
return (
Number(aNumber) - Number(zNumber) ||
(aNumber < zNumber ? -1 : 1)
)
}
return aCode - zCode
}
return a.length - z.length
}