JS,sort中文字段不生效。
见以下代码,我的需求是对其中两个字段进行排序(分类聚集)。主要条件是class(即把区域的放到一起、网格的放到一起),在排序好class的基础上,在升序after。
但不知道为什么class排序总不能生效。请各位大佬指教,感谢。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const arrData = [
{
"class": "区域",
"name": "茂南",
"before": 0.72826087,
"after": 0.831325301
},{
"class": "网格",
"name": "西城",
"before": 1,
"after": 0.944444444
},{
"class": "网格",
"name": "天堂",
"before": 0.740740741,
"after": 0.80141844
},{
"class": "区域",
"name": "公馆",
"before": 0.643678161,
"after": 0.75
},{
"class": "网格",
"name": "银河",
"before": 0.4,
"after": 0.62
},{
"class": "网格",
"name": "猎户",
"before": 0.476190476,
"after": 0.664634146
},{
"class": "区域",
"name": "仙女",
"before": 0.592592593,
"after": 0.753665689
},{
"class": "网格",
"name": "天门",
"before": 0.333333333,
"after": 0.525
},{
"class": "网格",
"name": "斗牛",
"before": 0.666666667,
"after": 0.756756757
},{
"class": "全市",
"name": "全市",
"before": 0.675675676,
"after": 0.784069982
}
];
console.log('排序前');
console.table(arrData);
console.log('排序后');
// 次要排序条件
arrData.sort((a,b)=>a.after-b.after);
// 首要排序条件
arrData.sort((a,b)=>a.class-b.class);
console.table(arrData);
</script>
</body>
</html> 你这是没有接收结果的变量吧? 这样就行了
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const arrData = [
{
"class": "区域",
"name": "茂南",
"before": 0.72826087,
"after": 0.831325301
}, {
"class": "网格",
"name": "西城",
"before": 1,
"after": 0.944444444
}, {
"class": "网格",
"name": "天堂",
"before": 0.740740741,
"after": 0.80141844
}, {
"class": "区域",
"name": "公馆",
"before": 0.643678161,
"after": 0.75
}, {
"class": "网格",
"name": "银河",
"before": 0.4,
"after": 0.62
}, {
"class": "网格",
"name": "猎户",
"before": 0.476190476,
"after": 0.664634146
}, {
"class": "区域",
"name": "仙女",
"before": 0.592592593,
"after": 0.753665689
}, {
"class": "网格",
"name": "天门",
"before": 0.333333333,
"after": 0.525
}, {
"class": "网格",
"name": "斗牛",
"before": 0.666666667,
"after": 0.756756757
}, {
"class": "全市",
"name": "全市",
"before": 0.675675676,
"after": 0.784069982
}
];
console.log('排序前');
console.table(arrData);
console.log('排序后');
arrData.sort((a, b) => {
let chIndex = a.class.localeCompare(b.class);
if (chIndex === 0) {
return a.before - b.before;
}
else {
return chIndex;
}
});
console.table(arrData);
</script>
</body>
</html> 支持支持 支持支持 中排序用 localeCompare 这个函数试试
console.log("排序前");
console.table(arrData);
console.log("排序后");
// 次要排序条件
arrData.sort((a, b) => a.after - b.after);
// 首要排序条件
arrData.sort((a, b) => a.class.localeCompare(b.class));
console.table(arrData); aizongke 发表于 2022-10-19 08:03
你这是没有接收结果的变量吧?
sort方法是改变原数组的。 <!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const arrData = [
{
"class": "区域",
"name": "茂南",
"before": 0.72826087,
"after": 0.831325301
},{
"class": "网格",
"name": "西城",
"before": 1,
"after": 0.944444444
},{
"class": "网格",
"name": "天堂",
"before": 0.740740741,
"after": 0.80141844
},{
"class": "区域",
"name": "公馆",
"before": 0.643678161,
"after": 0.75
},{
"class": "网格",
"name": "银河",
"before": 0.4,
"after": 0.62
},{
"class": "网格",
"name": "猎户",
"before": 0.476190476,
"after": 0.664634146
},{
"class": "区域",
"name": "仙女",
"before": 0.592592593,
"after": 0.753665689
},{
"class": "网格",
"name": "天门",
"before": 0.333333333,
"after": 0.525
},{
"class": "网格",
"name": "斗牛",
"before": 0.666666667,
"after": 0.756756757
},{
"class": "全市",
"name": "全市",
"before": 0.675675676,
"after": 0.784069982
}
];
console.log('排序前');
console.table(arrData);
console.log('排序后');
// 次要排序条件
arrData.sort((a,b)=>a.after-b.after);
// 首要排序条件
arrData.sort((a,b) => { return a.class.localeCompare(b.class,"zh"); });
console.table(arrData);
</script>
</body>
</html> sort方法是针对unicode编码的,不是按照汉语拼音顺序的,因为这个是外国人发明的。中文的比较方式是localCompare方法判断两个字符串的大小
页:
[1]