码迷,mamicode.com
首页 > 编程语言 > 详细

数组内的对象按照2个属性进行排序

时间:2016-08-12 01:27:26      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
数组内的对象按照2个属性进行排序(先按time从大到小排序,再按weight从大到小排序),请查看控制台看输出结果:<br/>
var array = [<br/>{time: 1, weight: 4},<br/> {time: 1, weight: 1}, <br/>{time: 1, weight: 9}, <br/>{time: 2, weight: 4},
<br/>{time: 2, weight: 1}, <br/>{time: 2, weight: 5}<br/>];
<script>
    //先按time从大到小排序,再按weight从大到小排序
    var array = [{time: 1, weight: 4}, {time: 1, weight: 1}, {time: 1, weight: 9}, {time: 2, weight: 4}, {time: 2,weight: 1}, {time: 2, weight: 5}];
    array.sort(function (a, b) {
        if (a.time !== b.time) {
            return b.time - a.time;
        } else {
            return b.weight - a.weight;
        }
    });
    var len = array.length;
    for (var i = 0; i < array.length; i++) {
        console.log("{time:" + array[i].time + ",weight:" + array[i].weight + "}\n");
    }
</script>
</body>
</html>

 

数组内的对象按照2个属性进行排序

标签:

原文地址:http://www.cnblogs.com/cag2050/p/5763294.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!