js json排序

时间:2022-08-27 17:14:21

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)