javascript中对中文字段排序

用javascript对数组中的中文字段进行排序

可以使用对象字面量来实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var o = [{id : 007, type : "广州"}, 
{id : 005, type : "深圳"},
{id : 008, type : "东莞"},
{id : 003, type : "中山"},
{id : 002, type : "深圳"},
{id : 006, type : "佛山"}];
var compareData = {
"东莞" : 1,
"佛山" : 2,
"广州" : 3,
"深圳" : 4,
"顺德" : 5,
"中山" : 6
};
l = o.sort(function compare(a,b){return compareData[a.type] - compareData[b.type];});
for(var i = 0; i < l.length; i++){
document.write(l[i]["type"]);
}

javascript中对中文字段排序
https://yangyewen.gitee.io/myblog/2022/08/05/javascript中对中文字段排序/
作者
Evan
发布于
2022年8月5日
许可协议