JavaScript实现中英文数字混合排序

/**
 * 中英文数字混合排序
 * @param {*} a
 * @param {*} b
 */
let arrSortMinToMax = (a, b) => {
  let cReg = /^[\u4E00-\u9FCC\u3400-\u4DB5\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\ud840-\ud868][\udc00-\udfff]|\ud869[\udc00-\uded6\udf00-\udfff]|[\ud86a-\ud86c][\udc00-\udfff]|\ud86d[\udc00-\udf34\udf40-\udfff]|\ud86e[\udc00-\udc1d]/
  if (!cReg.test(a.name) || !cReg.test(b.name)) {
    return a.name.localeCompare(b.name)
  } else {
    return a.name.localeCompare(b.name, "zh")
  }
}

let arr = [{ name: "d" }, { name: "江" }, { name: "8" }, { name: "j" }, { name: "1" }, { name: "水" }]
let sortArr = arr.sort(arrSortMinToMax)
console.log(sortArr) // [{ name: "1" }, { name: "8" }, { name: "江" }, { name: "水" }, { name: "d" }, { name: "j" }]

你可能感兴趣的:(#,JavaScript)