日常工作中常用方法记录
1.根据数组对象中的某一个属性进行排序
var arr = [ {name:'zopp',age:0}, {name:'gpp',age:18}, {name:'yjj',age:8} ]; function compare(property){ return function(a,b){ var value1 = a[property]; var value2 = b[property]; return value1 - value2; } } console.log(arr.sort(compare('age')))
2 数组对象中的日期进行排序
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); }