js的中文英文排序
本例主要实现 中文汉字按拼音排序的方法和英文按照首字母排序的方法。
//要排序的数据 let data = [ {chinese: '蔡司', english: 'Chase'}, {chinese: '艾伦', english: 'Allen'}, {chinese: '左拉', english: 'Zola'}, {chinese: '贝克', english: 'Baker'}, {chinese: '伯格', english: 'Berg'}, {chinese: '菲奇', english: 'Fitch'}, {chinese: '迪安', english: 'Dean'}, {chinese: '厄尔', english: 'Earle'}, {chinese: '亨利', english: 'Henry'}, ] 中文:data.sort((a, b)=> b.chinese.localeCompare(a.chinese, 'zh')); //z~a 排序 中文: data.sort((a, b)=> a.chinese.localeCompare(b.chinese, 'zh')); //a~z 排序 英文:data.sort((a, b) => b.english.charCodeAt(0) - a.english.charCodeAt(0)); //z~a 排序 英文:data.sort((a, b) => a.english.charCodeAt(0) - b.english.charCodeAt(0)); //a~z 排序