Js实现中文排序(解决node环境下不支持汉字排序)

javascript/jquery

浏览数:40

2019-9-1

  1. 汉字所对应的拼音 省略字典(网上找或者联系作者)

       const PinYin = {
             a: '\u554a\u963f\u9515',
             ai:
         }
    
  2. 在字典中搜索

     function arraySearch(zh) {
         for (let name in PinYin) {
             if (PinYin[name].indexOf(zh) != -1) {
                 return name
             }
         }
         return ''
     }
    
    
  3. 汉字转拼音

     function ConvertPinyin(str) {
         const reg = new RegExp(/[\u4e00-\u9fa5]/) //匹配汉字
         let ary = str.split('')
         let pinyin = ary.reduce((pre, item) => {
             if (reg.test(item)) {
                 let name = arraySearch(item)
                 pre += name
             } else {
                 pre += item
             }
             return pre
         }, '')
         let letter = pinyin.slice(0, 1)
         return { pinyin, letter }
     }
    
  4. 排序

     const pySegSort = (arr, keyword) => {
         if (!String.prototype.localeCompare) return null
         let obj = {}
         arr.forEach(item => {
             if (keyword) {
                 const { pinyin, letter } = ConvertPinyin(item[keyword])
                 obj[letter]
                     ? obj[letter].push({ ...item, pinyin, letter })
                     : (obj[letter] = [{ ...item, pinyin, letter }])
             } else {
                 const { pinyin, letter } = ConvertPinyin(item)
                 obj[letter]
                 ? obj[letter].push({ zh: item, pinyin, letter })
                 : (obj[letter] = [{ zh: item, pinyin, letter }])
             }
         })
         let data=Object.keys(obj).sort((a,b)=>a.localeCompare(b)).map(item=>{
             let data =obj[item]
             data.sort((a,b)=>a.pinyin.localeCompare(b.pinyin))
             return {title:item,data}
         })
         return data
     }
    
  5. 测试

    let a = [“保定”,”西安”,”澳门”,”台湾”,’江苏’,”承德”,”抚顺”,”海南”,’西藏’,’天津’,”宝鸡”,”上海”,”北京”]
    pySegSort(a)

     [
         {   title: 'a', data: [{ zh: '澳门', pinyin: 'aomen', letter: 'a' }] },
         {
             title: 'b',
             data: [
                 { zh: '保定', pinyin: 'baoding', letter: 'b' },
                 { zh: '宝鸡', pinyin: 'baoji', letter: 'b' },
                 { zh: '北京', pinyin: 'beijing', letter: 'b' },
             ],
         },
         { title: 'c', data: [{ zh: '承德', pinyin: 'chengde', letter: 'c' }] },
         { title: 'f', data: [{ zh: '抚顺', pinyin: 'fushun', letter: 'f' }] },
         { title: 'h', data: [{ zh: '海南', pinyin: 'hainan', letter: 'h' }] },
         { title: 'j', data: [{ zh: '江苏', pinyin: 'jiangsu', letter: 'j' }] },
         { title: 's', data: [{ zh: '上海', pinyin: 'shanghai', letter: 's' }] },
         {
             title: 't',
             data: [
                 { zh: '台湾', pinyin: 'taiwan', letter: 't' },
                 { zh: '天津', pinyin: 'tianjin', letter: 't' },
             ],
         },
         {
             title: 'x',
             data: [
                 { zh: '西安', pinyin: 'xian', letter: 'x' },
                 { zh: '西藏', pinyin: 'xicang', letter: 'x' },
             ],
         },
     ]
    
    
    

作者:Tesla