let arr = [
{ch: "我", en: "me"},
{ch: "你", en: "your"},
{ch: "他", en: "he"},
{ch: "不不", en: "no"},
]
// 转为unicode 编码
function encodeUnicode(str) {
var res = [];
for ( var i=0; i<str.length; i++ ) {
res[i] = ( "00" + str.charCodeAt(i).toString(16) ).slice(-4);
}
return "\\u" + res.join("\\u");
}
// 转为unicode 编码 如果不带\u
function encodeUnicode(str) {
var res = [];
for ( var i=0; i<str.length; i++ ) {
res[i] = ( "00" + str.charCodeAt(i).toString(16) ).slice(-4);
}
return res.join("");
}
arr.forEach((item) => {
console.log(encodeUnicode(item.ch))
// \u6211 \u4f60 \u4ed6 \u4e0d\u4e0d
})
let _arr = arr.sort((a,b) => {
let _a = encodeUnicode(a.ch)
let _b = encodeUnicode(b.ch)
return _a.localeCompare(_b)
})
console.log(_arr) // 中文排序后的数组
let arr2 = arr.sort((a,b) => {
let _a = a.en
let _b = b.en
return _a.localeCompare(_b)
})
console.log(arr2) // en排序后的数组
// 数字排序:
let _arr = arr.sort((a,b) => a - b )
console.log(_arr)
// 字符串排序:
let _arr = arr.sort((a,b) => {
return a.localeCompare(b)
})
console.log(_arr)