• json sort


    Array.sort()方法是用来对数组项进行排序的 ,默认情况下是进行升序排列。sort() 方法可以接受一个 方法为参数。

    sort()排序时每次比较两个数组项都回执行这个参数,并把两个比较的数组项作为参数传递给这个函数。当函数返回值为1的时候就交换两个数组项的顺序,否则就不交换。

    var p = [5, 2, 3, 1, 7, 5, 6, 9, 6, 0];
              function down(a, b) {
                  return   (a < b) ? 1 : -1
              }
              p.sort(down)
              alert(p)

     json排序

    var p = [
                {name:"kitty", age:12},
                {name:"sonny", age:9},
                {name:"jake", age:13},
                {name:"fun", age:24}
            ]
            function down(x, y) {
                return (x.age < y.age) ? 1 : -1
     
            }
            p.sort(down)
            var $text = "<div>"
            $.each(p, function (key, value) {
                var $div = "<div>"
                $.each(value, function (key, value) {
                    $div += "<span>" + key + ":</span>" "<span>" + value + "</span>" "         "
                })
                $div += "</div>"
                $text = $text + $div
            })
            $text += "</div>"
     
     
     
            $(".text").html($text)

     写成类

    <script type="text/javascript">
        $(document).ready(function () {
            var p = [
                {name:"kitty", age:12, price:190},
                {name:"sonny", age:9, price:390},
                {name:"jake", age:13, price:42},
                {name:"fun", age:24, price:210}
            ]
     
            var tablesort = {
                init:function (arry, parm, sortby) {
                    this.obj = arry
                    this.parm = parm
                    this.b = sortby
                },
     
                sot:function () {
                    var $this this
                    var down = function (x, y) {
                        return (eval("x." + $this.parm) > eval("y." + $this.parm)) ? -1 : 1
                    }//通过eval对json对象的键值传参
                    var up = function (x, y) {
                        return (eval("x." + $this.parm) < eval("y." + $this.parm)) ? -1 : 1
                    }
                    if (this.b == "down") {
                        this.obj.sort(down)
                    }
                    else {
                        this.obj.sort(up)
                    }
     
                },//排序
     
                prin:function () {
                    var $text = "<div>"
                    $.each(this.obj, function (key, value) {
                        var $div = "<div>"
                        $.each(value, function (key, value) {
                            $div += "<span>" + key + ":</span>" "<span>" + value + "</span>" "         "
                        })
                        $div += "</div>"
                        $text = $text + $div
                    })
                    $text += "</div>"
                    $("html body").html($text)
                }//遍历添加dom元素,添加dom
            }
     
            function _temp() {
                this.init.apply(this, arguments)
            }
     
            _temp.prototype = tablesort;
            var sort1 = new _temp(p, "price""down"//建立对象
            sort1.init(p, "age""up");//初始化参数更改
            sort1.sot()
            sort1.prin()
     
        })
     
     
    </script>
  • 相关阅读:
    cef 依赖的文件 libcef
    ubuntu 添加新分区,并挂载/home
    x509 证书链验证
    chromium调试
    Chromium智能指针使用指南
    具有扩展主密钥时SSL/TLS的主密钥计算
    OpenSSL解惑2:如何强制选择协议的版本
    基于Gmssl的SM2加解密算法Demo
    漫谈TLS nonce
    VS2015动态库静态加载时指定动态库位置的方法
  • 原文地址:https://www.cnblogs.com/WarBlog/p/5212078.html
Copyright © 2020-2023  润新知