'七八九四五六一二三'.split('').sort()
[
"一", "七", "三",
"九", "二", "五",
"八", "六", "四"
]
'七八九四五六一二三'.split('').sort(function(a, b){
return a.localeCompare(b, ['zh-Hant-TW'])
})
[
"一", "七", "九",
"二", "八", "三",
"五", "六", "四"
]
'七八九四五六一二三'.split('').sort(function(a, b){
return a.localeCompare(b, ['zh-Hant-TW-u-co-pinyin'])
})
[
"八", "二", "九",
"六", "七", "三",
"四", "五", "一"
]
'七八九四五六一二三'.split('').sort(function(a, b){
return a.localeCompare(b, ['zh-Hant-TW-u-co-zhuyin'])
})
[
"八", "二", "九",
"六", "七", "三",
"四", "五", "一"
]
參考資料
黑暗執行緒, (2017, October 19). JavaScript 中文排序問題. 黑暗執行緒 Blog. https://blog.darkthread.net/blog/javascript-chinese-char-sorting/
String.prototype.localeCompare() - JavaScript . MDN. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
沒有留言:
張貼留言