JesuisTong / someTest

lab from TongZ, there are just some experimental tests
1 stars 0 forks source link

简单手写一个unicode码点排序函数 #21

Open JesuisTong opened 2 years ago

JesuisTong commented 2 years ago
// 简单手写一个unicode码点排序函数
// https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

function unicodeSort(a, b) {
    let aa = a.toString()
    let bb = b.toString()

    let index = 0;
    do {
        let charA = aa.charCodeAt(index)
        let charB = bb.charCodeAt(index)
        if (charA === charB) {
            index++
            continue
        } else if (Number.isNaN(charA) && Number.isNaN(charB)) {
            return 0
        } else if (Number.isNaN(charA)) {
            return -1
        } else if (Number.isNaN(charB)) {
            return 1
        } else {
            return charA - charB
        }
    } while(true)
}