数组双重排序的方法

做到双重排序时,原数据如下,需要对star和whether进行排序,在做项目时,star代表星级,在星级从大到小的情况下在再对whether进行排序, 代码如下:

     let tableData = [
        {
          date: '04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 5,
          whether: 1
        },
        {
          date: '05',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 5,
          whether: 1
        },
        {
          date: '06',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 5,
          whether: 0
        },
        {
          date: '07',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 5,
          whether: 1
        },
        {
          date: '08',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 5,
          whether: 0
        },
        {
          date: '09',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 5,
          whether: 1
        },
        {
          date: '11',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 3,
          whether: 0
        },
        {
          date: '12',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 3,
          whether: 0
        },
        {
          date: '13',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 3,
          whether: 1
        },
        {
          date: '14',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 3,
          whether: 1
        },
        {
          date: '15',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 3,
          whether: 1
        },
        {
          date: '16',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 3,
          whether: 0
        },
        {
          date: '17',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 3,
          whether: 0
        },
        {
          date: '18',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 3,
          whether: 1
        },
        {
          date: '19',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 3,
          whether: 1
        },
        {
          date: '20',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 3,
          whether: 0
        },
        {
          date: '01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄',
          star: 2,
          whether: 0
        },
        {
          date: '02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄',
          star: 2,
          whether: 0
        },
        {
          date: '03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄',
          star: 2,
          whether: 1
        },
        {
          date: '10',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          star: 5,
          whether: 1
        }
      ]

     tableData.sort((x, y) => {
        // 如果星级相同进行 whether 之间的排序
        if (x.star === y.star) {
          return y.whether - x.whether
        } else {
          //星级不同时直接以星级进行排序
          return y.star - x.star
        }
      })

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值