zoukankan      html  css  js  c++  java
  • js sort tricks All In One

    js sort tricks All In One

    js 排序技巧

    const arr = [
        { label: 'False 1 ❌', disabled: false, },
        { label: 'False 2 ❌', disabled: false },
        { label: 'Ture 1 ✅', disabled: true, },
        { label: 'False 3 ❌', disabled: false, },
        { label: 'Ture 2 ✅', disabled: true, },
        { label: 'Ture 3 ✅', disabled: true, },
        { label: 'False 4 ❌', disabled: false, },
    ];
    
    // JSON.stringify(arr, null, 4);
    
    
    const subTableList = () => {
        // sort 排序里面的函数意思是 按照 disabled 属性(先 false 后 true) 的方式排序, 并按照索引的先后顺序不变
        const temp = [...arr];
        // log(`
    temp 1`, temp);
        // return temp.sort((a, b) => (a.disabled ^ b.disabled ? 1 : 0) * (a.disabled ? 1 : -1));
        return temp.sort((a, b) => (a.disabled ^ b.disabled) * a.disabled);
        // return temp.sort((a, b) => (a.disabled ^ b.disabled) * +a.disabled);
    }
    
    const MapList = () => {
        const temp = [...arr];
        // log(`
    temp 2`, temp);
        // const result = new Map();
        const result = [];
        for (let i = 0; i < temp.length; i++) {
            const item = temp[i];
            if(!item.disabled) {
                result.push(item);
            } else {
                result.unshift(item);
            }
        }
        return result;
        // return temp.map((a, b) => (a.disabled ^ b.disabled ? 1 : 0) * (a.disabled ? 1 : -1));
        // return temp.sort((a, b) => (a.disabled ^ b.disabled ? 1 : 0) * (a.disabled ? 1 : -1));
    }
    
    const log = console.log;
    
    
    log(`subTableList =
    `, subTableList());
    
    
    log(`
    MapList =
    `, MapList());
    
    
    /* 
    
    node map.js
    subTableList =
     [ { label: 'False 1 ❌', disabled: false },
      { label: 'False 2 ❌', disabled: false },
      { label: 'False 3 ❌', disabled: false },
      { label: 'False 4 ❌', disabled: false },
      { label: 'Ture 1 ✅', disabled: true },
      { label: 'Ture 2 ✅', disabled: true },
      { label: 'Ture 3 ✅', disabled: true } ]
    
    MapList =
     [ { label: 'Ture 3 ✅', disabled: true },
      { label: 'Ture 2 ✅', disabled: true },
      { label: 'Ture 1 ✅', disabled: true },
      { label: 'False 1 ❌', disabled: false },
      { label: 'False 2 ❌', disabled: false },
      { label: 'False 3 ❌', disabled: false },
      { label: 'False 4 ❌', disabled: false } ]
      
    */
    
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

    js ^ operator

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_XOR

    
    const a = 1;        // 00000000000000000000000000000001
    const b = 3;        // 00000000000000000000000000000011
    
    console.log(a ^ b); // 00000000000000000000000000000010
    // 2
    
    // ^ 异或, 11 的 0, 01,10 的 1,00 的 0
    
    
    1 * true;
    1
    1 * false;
    0
    
    0 * true;
    0
    0 * false;
    0
    
    
    (false ^ true);
    1
    (true ^ false);
    1
    
    (false ^ false);
    0
    (true ^ true);
    0
    
    (false ^ true) * false;
    1
    (false ^ true) ^ true;
    0
    
    
    !false;
    true
    !!false;
    false
    
    !true;
    false
    !!true;
    true
    
    +false;
    0
    -false;
    -0
    
    +true;
    1
    -true;
    -1
    
    

    refs



    ©xgqfrms 2012-2020

    www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


    xgqfrms
  • 相关阅读:
    【转】总体样本方差的无偏估计样本方差为什么除以n-1
    【转】用PyQt5写的第一个程序
    向量的协方差计算
    数据挖掘模型
    数据挖掘方法论及实施步骤
    Hadoop MapReduce八大步骤以及Yarn工作原理详解
    传统数据仓库架构与Hadoop的区别
    数据库优化方案整理
    数仓interview总结
    十四个值得推荐的个人提升方法
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/14313574.html
Copyright © 2011-2022 走看看