`
tangzongyun
  • 浏览: 167884 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

JavaScript 对象数组 按照某个属性 降序排列

    博客分类:
  • js
阅读更多
<!DOCTYPE html>
<html>

<body>
<script>
//定义一个对象数组
   
var data = [{
name: "海外事业部",
value: 0.58
}, {
name: "内销",
value: 0.36
}, {
name: "互联网中心",
value: 0.78
}];   
//定义一个比较器--降序排列
function compare(propertyName) {
return function(object1, object2) {
var value1 = object1[propertyName];
var value2 = object2[propertyName];
if(value2 < value1) {
return -1;
} else if(value2 > value1) {
return 1;
} else {
return 0;
}
}
}
console.info(JSON.stringify(data));
//使用方法
data.sort(compare("value")); 
console.info(JSON.stringify(data)); 
</script>

<h1>JavaScript 对象数组 按照某个属性 降序排列</h1>

<p>
JavaScript 对象数组 按照某个属性 降序排列
</p>



</body>

</html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics