排序理解

sort排序时,List中存在两个相同元素,两个元素相同时,按照下标的先后顺序排序。

<script>
var list = [
{
username:'758274',
count:9,
count2:8,
time:30,
time2:30,
},
{
username:'758275',
count:5,
count2:10,
time:30,
time2:30,
}
];
list = list.sort(function(a,b){
return (b.count * b.time + b.count2* b.time2) /(b.count+b.count2) - (a.count * a.time + a.count2* a.time2) /(a.count+a.count2)
});
console.log(list);
</script>
 
原文地址:https://www.cnblogs.com/qcq0703/p/11356688.html