compare(property) { return function(a, b) { var value1 = a[property]; var value2 = b[property]; return value1 - value2; } },
排序前的json对象
<script> var json1 = [ {"name":"小明","avg":"80"}, {"name":"小红","avg":"70"}, {"name":"小绿","avg":"90"} ] console.log(json1); </script>
使用上面给出的排序函数:
var json2 = json1.sort(compare("avg")); console.log(json2)
排序后的json数组:
tips:这样操作后 原数组的顺序也会被改变 使用的时候要注意了
时间: 2024-10-16 13:01:20