• JS数组排序


    ✨求关注~
    😀博客:www.protaos.com

    基础版本

    要处理一个数组,根据数组对象内元素的 “status” 属性值从小到大排序,你可以使用 JavaScript 的 Array.prototype.sort() 方法。以下是一个示例代码,假设你有一个包含对象的数组 data,每个对象都有一个名为 “status” 的属性:

    // 示例数据数组
    const data = [
      { name: "Item 1", status: 3 },
      { name: "Item 2", status: 1 },
      { name: "Item 3", status: 2 },
      // ... 更多对象
    ];
    
    // 使用 Array.prototype.sort() 方法根据 "status" 属性排序
    data.sort((a, b) => a.status - b.status);
    
    // 排序后的数组
    console.log(data);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    这段代码首先定义了一个示例数据数组 data,然后使用 data.sort() 方法,传入一个比较函数,该函数会比较每个对象的 “status” 属性,从而对数组进行排序。最终,数组将按 “status” 属性的值从小到大排序

    如果需要按升序或降序排序,只需相应地更改比较函数中的比较逻辑。如果要按降序排序,可以将 a.status - b.status 更改为 b.status - a.status

    示例中的数组和属性名仅供参考,请根据你的实际数据结构进行相应的调整。

    进阶版本

    如果在内部在增加一个 id 的参数、!id&&status==0的时候返回为最小值

    如果你想在排序时考虑一个名为 “id” 的参数,同时在 “status” 为 0 且没有 “id” 值的情况下将它视为最小值,你可以稍微修改比较函数。以下是一个示例代码:

    // 示例数据数组
    const data = [
      { name: "Item 1", status: 3, id: 1 },
      { name: "Item 2", status: 0 },
      { name: "Item 3", status: 2, id: 3 },
      { name: "Item 4", status: 0 },
      // ... 更多对象
    ];
    
    // 使用 Array.prototype.sort() 方法根据 "status" 属性排序
    data.sort((a, b) => {
      // 如果其中一个对象的 status 为 0 且没有 id,将其视为最小值
      if (!a.id && a.status === 0) {
        return -1; // a 小于 b
      }
      if (!b.id && b.status === 0) {
        return 1; // b 小于 a
      }
    
      // 否则,按照 status 属性进行排序
      return a.status - b.status;
    });
    
    // 排序后的数组
    console.log(data);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    这段代码首先检查每个对象的 “id” 和 “status” 属性,如果其中一个对象的 “status” 为 0 且没有 “id” 属性,则将其视为最小值。否则,将按照 “status” 属性的值进行排序。这样,数组将首先根据这两个条件排序,然后按照 “status” 属性的值排序。

    反向排序

    如果你希望在最后对数组进行反向排序,你可以首先按照 “status” 属性进行升序排序,然后使用 Array.prototype.reverse() 方法来颠倒数组的顺序。以下是修改后的示例代码:

    // 示例数据数组
    const data = [
      { name: "Item 1", status: 3, id: 1 },
      { name: "Item 2", status: 0 },
      { name: "Item 3", status: 2, id: 3 },
      { name: "Item 4", status: 0 },
      // ... 更多对象
    ];
    
    // 使用 Array.prototype.sort() 方法根据 "status" 属性进行升序排序
    data.sort((a, b) => {
      // 如果其中一个对象的 status 为 0 且没有 id,将其视为最小值
      if (!a.id && a.status === 0) {
        return -1; // a 小于 b
      }
      if (!b.id && b.status === 0) {
        return 1; // b 小于 a
      }
    
      // 否则,按照 status 属性进行排序
      return a.status - b.status;
    });
    
    // 使用 Array.prototype.reverse() 方法反向排序数组
    data.reverse();
    
    // 反向排序后的数组
    console.log(data);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    这段代码首先按照 “status” 属性进行升序排序,然后使用 reverse() 方法来反向排序数组,从而得到了按 “status” 属性升序排列并最后反向排序的结果。

  • 相关阅读:
    【附源码】计算机毕业设计JAVA婚纱摄影管理
    node.js——模块化
    如何运营推特营销主账号
    C语言解题——指针解析(牛客网题目)
    PCL RANSAC分割提取多个空间圆
    springboot197基于springboot的毕业设计系统的开发
    力扣每日一道系列 --- LeetCode 88. 合并两个有序数组
    C Primer Plus(6) 中文版 第14章 结构和其他数据形式 14.11 枚举类型
    test1
    MySQL数据库事务
  • 原文地址:https://blog.csdn.net/qq_43784821/article/details/133905898