localeCompare()
1.数组内的元素是中文字符串的简单排序
var arr = ['南京', '北京', '上海', '杭州', '深圳'];
function sortChinese (arr) { // 参数: 排序的数组
arr.sort(function (item1, item2) {
return item1.localeCompare(item2, 'zh-CN');
})
}
sortChinese(arr)
console.log(arr); // ["北京", "杭州", "南京", "上海", "深圳"]
2.数组内的元素是对象,以对象某一个属性进行排序
var arr = [
{name: '南京', code: '09', info: {province: '江苏'}},
{name: '北京', code: '01', info: {province: '北京'}},
{name: '上海', code: '02', info: {province: '上海'}},
{name: '深圳', code: '05', info: {province: '广东'}}
];
function sortChinese (arr, dataLeven) { // 参数:arr 排序的数组; dataLeven 数组内的需要比较的元素属性
/* 获取数组元素内需要比较的值 */
function getValue (option) { // 参数: option 数组元素
if (!dataLeven) return option
var data = option
dataLeven.split('.').filter(function (item) {
data = data[item]
})
return data + ''
}
arr.sort(function (item1, item2) {
return getValue(item1).localeCompare(getValue(item2), 'zh-CN');
})
}
sortChinese(arr, 'name') // 例如:比较的是name,传入的就是 'name'
console.log(arr);
/*[{name: '北京', code: '01', info: {province: '北京'}},
{name: '南京', code: '09', info: {province: '江苏'}},
{name: '上海', code: '02', info: {province: '上海'}},
{name: '深圳', code: '05', info: {province: '广东'}}]*/
sortChinese(arr, 'info.province') // 例如:比较的是数组元素属性info内的province属性,传入的就是 'info.province'
console.log(arr);
/* [{name: '北京', code: '01', info: {province: '北京'}},
{name: '深圳', code: '05', info: {province: '广东'}},
{name: '南京', code: '09', info: {province: '江苏'}},
{name: '上海', code: '02', info: {province: '上海'}}]*/
3.对国内的所有省份进行排序,并且首字母相同的第一个添加首字母
var provinceList = [
{"code": "82", "value": "澳门"},
{"code": "34", "value": "安徽"},
{"code": "11", "value": "北京"},
{"code": "50", "value": "重庆"},
{"code": "35", "value": "福建"},
{"code": "62", "value": "甘肃"},
{"code": "44", "value": "广东"},
{"code": "45", "value": "广西"},
{"code": "52", "value": "贵州"},
{"code": "46", "value": "海南"},
{"code": "13", "value": "河北"},
{"code": "41", "value": "河南"},
{"code": "23", "value": "黑龙江"},
{"code": "42", "value": "湖北"},
{"code": "43", "value": "湖南"},
{"code": "22", "value": "吉林"},
{"code": "32", "value": "江苏"},
{"code": "36", "value": "江西"},
{"code": "21", "value": "辽宁"},
{"code": "15", "value": "内蒙古"},
{"code": "64", "value": "宁夏"},
{"code": "63", "value": "青海"},
{"code": "37", "value": "山东"},
{"code": "14", "value": "山西"},
{"code": "61", "value": "陕西"},
{"code": "31", "value": "上海"},
{"code": "51", "value": "四川"},
{"code": "71", "value": "台湾"},
{"code": "12", "value": "天津"},
{"code": "54", "value": "西藏"},
{"code": "81", "value": "香港"},
{"code": "65", "value": "新疆"},
{"code": "53", "value": "云南"},
{"code": "33", "value": "浙江"}
]
View Code
function chineseLetter (arr, dataLeven) {
var letter = 'abcdefghjklmnopqrstwxyz'.split('')
var zh = "阿八嚓哒妸发旮哈讥咔垃痳拏噢妑七呥扨它穵夕丫帀".split('')
/* 获取数组元素比较的值 */
function getValue (option) {
if (!dataLeven) return option
var data = option
dataLeven.split('.').filter(function (item) {
data = data[item]
})
return data + ''
}
/* 进行排序 */
arr.sort(function (item1, item2) {
return getValue(item1).localeCompare(getValue(item2), 'zh-Hans-CN')
})
/* 判断需要排序的字符串是否含有中文字符 */
if (/[\u4e00-\u9fff]/.test(getValue(arr[0])) && typeof arr[0] === 'object') pySegSort(0, 0)
/* 给省列表中添加首字符 */
function pySegSort (letterIndex, zhIndex) {
var first = true // 首次是否加 字母标识
for (var i = zhIndex; i < arr.length; i++) {
var item = arr[i]
// 是否有值 && 当前值大于等于本次字母的最小值 && (最后一位 || 当前值小于下次字母的最小值)
var state = zh[letterIndex] && getValue(item).localeCompare(zh[letterIndex], 'zh') >= 0 && (letterIndex === letter.length - 1 || getValue(item).localeCompare(zh[letterIndex+1], 'zh') < 0)
if (state) { // 满足条件,同一个首字母下的:例如 A 下的所有省份
if (first) { //是否是第一次出现
item.letter = letter[letterIndex].toUpperCase()
first = false
} else {
item.letter = ''
}
} else { // 递归调用 函数,进行下次字母下的排列
letterIndex++
if (letterIndex < letter.length) {
pySegSort(letterIndex, i)
break
}
}
}
}
}
chineseLetter(provinceList, 'value')
console.log(provinceList)
// 结果如下:
[
{code: "34", value: "安徽", letter: "A"}
{code: "82", value: "澳门", letter: ""}
{code: "11", value: "北京", letter: "B"}
{code: "50", value: "重庆", letter: "C"}
{code: "35", value: "福建", letter: "F"}
{code: "62", value: "甘肃", letter: "G"}
{code: "44", value: "广东", letter: ""}
{code: "45", value: "广西", letter: ""}
{code: "52", value: "贵州", letter: ""}
{code: "46", value: "海南", letter: "H"}
{code: "13", value: "河北", letter: ""}
{code: "41", value: "河南", letter: ""}
{code: "23", value: "黑龙江", letter: ""}
{code: "42", value: "湖北", letter: ""}
{code: "43", value: "湖南", letter: ""}
{code: "22", value: "吉林", letter: "J"}
{code: "32", value: "江苏", letter: ""}
{code: "36", value: "江西", letter: ""}
{code: "21", value: "辽宁", letter: "L"}
{code: "15", value: "内蒙古", letter: "N"}
{code: "64", value: "宁夏", letter: ""}
{code: "63", value: "青海", letter: "Q"}
{code: "37", value: "山东", letter: "S"}
{code: "14", value: "山西", letter: ""}
{code: "61", value: "陕西", letter: ""}
{code: "31", value: "上海", letter: ""}
{code: "51", value: "四川", letter: ""}
{code: "71", value: "台湾", letter: "T"}
{code: "12", value: "天津", letter: ""}
{code: "54", value: "西藏", letter: "X"}
{code: "81", value: "香港", letter: ""}
{code: "65", value: "新疆", letter: ""}
{code: "53", value: "云南", letter: "Y"}
{code: "33", value: "浙江", letter: "Z"}
]
View Code