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

json对象按时间排序

时间:2018-02-27 19:29:32      阅读:576      评论:0      收藏:0      [点我收藏+]

标签:parse   ret   var   排序   blog   name   body   json   turn   

//正序
var data = {
"rows": [{
"name": "张三",
"time": "2011/4/1 0:00:00",
},{
"name": "李四",
"time": "2015/5/6 12:30:00",
},{
"name": "王五",
"time": "2012/10/1 22:10:00",
},{
"name": "赵六",
"time": "2011/9/1 22:10:00",
}]
};
var rows = data.rows;
rows.sort(function(a,b){
return Date.parse(a.time) - Date.parse(b.time);//时间正序
});
for(var i =0,l=rows.length;i<l;i++){
console.log(rows[i].name + " | " + rows[i].time);
}
//倒序
var data = {
"rows": [{
"name": "张三",
"time": "2011/4/1 0:00:00",
},{
"name": "李四",
"time": "2015/5/6 12:30:00",
},{
"name": "王五",
"time": "2012/10/1 22:10:00",
},{
"name": "赵六",
"time": "2011/9/1 22:10:00",
}]
};
var rows = data.rows;
rows.sort(function(a,b){
return Date.parse(b.time)-Date.parse(a.time);//时间正序
});
for(var i =0,l=rows.length;i<l;i++){
console.log(rows[i].name + " | " + rows[i].time);
}

json对象按时间排序

标签:parse   ret   var   排序   blog   name   body   json   turn   

原文地址:https://www.cnblogs.com/horanly/p/8480081.html

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