标签:
<!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>
标签:
原文地址:http://www.cnblogs.com/cag2050/p/5763294.html