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
  • 相关阅读:
    codeforces C. Cows and Sequence 解题报告
    codeforces A. Point on Spiral 解题报告
    codeforces C. New Year Ratings Change 解题报告
    codeforces A. Fox and Box Accumulation 解题报告
    codeforces B. Multitasking 解题报告
    git命令使用
    shell简单使用
    知识束缚
    php 调用系统命令
    数据传输方式(前端与后台 ,后台与后台)
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/14313574.html
Copyright © 2011-2022 走看看