数组,对象,json 排序

<!DOCTYPE html>
<html>
<head>
    <title>数组,对象,json 排序</title>
</head>
<body>

    <script type="text/javascript">

        var arr = [
            {'name':'科长','val':72},
            {'name':'明亮','val':122},
            {'name':'小K','val':28},
            {'name':'大米','val':112}
        ];

        function getSort(a,b){
            return b.val-a.val
        }

        console.log('排序前->',JSON.stringify(arr_))

        arr.sort(getSort);

        console.log('排序后->',JSON.stringify(arr));

    </script>

</body>
</html>

输出:

排序前-> [{"name":"科长","val":72},{"name":"明亮","val":122},{"name":"小K","val":28},{"name":"大米","val":112}]

排序后-> [{"name":"明亮","val":122},{"name":"大米","val":112},{"name":"科长","val":72},{"name":"小K","val":28}]