zoukankan      html  css  js  c++  java
  • js sort方法根据数组中对象的某一个属性值进行排序

    var arr = [
        {name:'zopp',age:0},
        {name:'gpp',age:18},
        {name:'yjj',age:8}
    ];
     
    function compare(property){
        return function(a,b){
            var value1 = a[property];
            var value2 = b[property];
            return value1 - value2;
        }
    }
    console.log(arr.sort(compare('age')))

    如何根据参数不同,来确定是升序排列,还是降序排序呢?

    /**数组根据数组对象中的某个属性值进行排序的方法
         * 使用例子:newArray.sort(sortBy('number',false)) //表示根据number属性降序排列;若第二个参数不传递,默认表示升序排序
         * @param attr 排序的属性 如number属性
         * @param rev true表示升序排列,false降序排序
         * */
        sortBy: function(attr,rev){
            //第二个参数没有传递 默认升序排列
            if(rev ==  undefined){
                rev = 1;
            }else{
                rev = (rev) ? 1 : -1;
            }
             
            return function(a,b){
                a = a[attr];
                b = b[attr];
                if(a < b){
                    return rev * -1;
                }
                if(a > b){
                    return rev * 1;
                }
                return 0;
            }
        }

    来源:https://www.cnblogs.com/deepalley/p/13559339.html

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    var arr = [
        {name:'zopp',age:0},
        {name:'gpp',age:18},
        {name:'yjj',age:8}
    ];
     
    function compare(property){
        return function(a,b){
            var value1 = a[property];
            var value2 = b[property];
            return value1 - value2;
        }
    }
    console.log(arr.sort(compare('age')))

      

    如何根据参数不同,来确定是升序排列,还是降序排序呢?

    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
    /**数组根据数组对象中的某个属性值进行排序的方法
         * 使用例子:newArray.sort(sortBy('number',false)) //表示根据number属性降序排列;若第二个参数不传递,默认表示升序排序
         * @param attr 排序的属性 如number属性
         * @param rev true表示升序排列,false降序排序
         * */
        sortBy: function(attr,rev){
            //第二个参数没有传递 默认升序排列
            if(rev ==  undefined){
                rev = 1;
            }else{
                rev = (rev) ? 1 : -1;
            }
             
            return function(a,b){
                a = a[attr];
                b = b[attr];
                if(a < b){
                    return rev * -1;
                }
                if(a > b){
                    return rev * 1;
                }
                return 0;
            }
        }
  • 相关阅读:
    mysql_wp_replication_tutorial
    Procedure execution failed 2013
    [Err] 1136
    innodb_flush_log_at_trx_commit和sync_binlog 参数说明
    mysql没有oracle 那样一次性把data buffer 数据写入磁盘
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field
    MyEclipse之Widget is disposed
    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Query was empty
    An internal error occurred during: "Building workspace". GC overhead limit exceeded
    Oracle 内部复制文档解读
  • 原文地址:https://www.cnblogs.com/sherryweb/p/15642171.html
Copyright © 2011-2022 走看看