吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1097|回复: 8
收起左侧

[求助] JS,sort中文字段不生效。

[复制链接]
cqwcns 发表于 2022-10-19 07:40
见以下代码,我的需求是对其中两个字段进行排序(分类聚集)。
主要条件是class(即把区域的放到一起、网格的放到一起),在排序好class的基础上,在升序after。


但不知道为什么class排序总不能生效。请各位大佬指教,感谢。

屏幕截图 2022-10-19 073944.png

[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)=>a.after-b.after);
            // 首要排序条件
			arrData.sort((a,b)=>a.class-b.class);
            console.table(arrData);

		</script>
	</body>

</html>

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
yuehanoo + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

aizongke 发表于 2022-10-19 08:03
你这是没有接收结果的变量吧?
icysky0605 发表于 2022-10-19 08:24
这样就行了
[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>

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
cqwcns + 1 + 1 谢谢@Thanks!

查看全部评分

aa2923821a 发表于 2022-10-19 08:42
x131797 发表于 2022-10-19 08:49
支持支持
lic121436 发表于 2022-10-19 08:49
中排序用 localeCompare 这个函数试试
[JavaScript] 纯文本查看 复制代码
      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);

免费评分

参与人数 1吾爱币 +1 收起 理由
cqwcns + 1 谢谢@Thanks!

查看全部评分

 楼主| cqwcns 发表于 2022-10-19 09:36
aizongke 发表于 2022-10-19 08:03
你这是没有接收结果的变量吧?

sort方法是改变原数组的。
w460270218 发表于 2022-10-19 09:36
[JavaScript] 纯文本查看 复制代码
<!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>

免费评分

参与人数 1热心值 +1 收起 理由
cqwcns + 1 谢谢@Thanks!

查看全部评分

qushushu 发表于 2022-10-19 10:57
sort方法是针对unicode编码的,不是按照汉语拼音顺序的,因为这个是外国人发明的。中文的比较方式是localCompare方法判断两个字符串的大小
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 07:00

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表