這樣:
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
console.log(rows[i].name + " | " + rows[i].time);
}
擴充套件資料:
注意事項
1、獲取兩個陣列時間的字串,轉成時間格式比較大小 [(NSDate*) compare:(NSDate*)]
2、根據時間大小排序此陣列 sortedArrayUsingComparator
(NSMutableArray *)[arrData sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *string1 = obj1[@"time"]; //獲取時間
NSString *string2 = obj2[@"time"];
NSDate *date1 = [formatter dateFromString:string1];
NSDate *date2 = [formatter dateFromString:string2];
NSComparisonResult result = [date1 compare:date2]; //比較時間大小
return result == NSOrderedAscending; //倒敘排列 返回陣列
}];
這樣:
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
console.log(rows[i].name + " | " + rows[i].time);
}
擴充套件資料:
注意事項
1、獲取兩個陣列時間的字串,轉成時間格式比較大小 [(NSDate*) compare:(NSDate*)]
2、根據時間大小排序此陣列 sortedArrayUsingComparator
(NSMutableArray *)[arrData sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *string1 = obj1[@"time"]; //獲取時間
NSString *string2 = obj2[@"time"];
NSDate *date1 = [formatter dateFromString:string1];
NSDate *date2 = [formatter dateFromString:string2];
NSComparisonResult result = [date1 compare:date2]; //比較時間大小
return result == NSOrderedAscending; //倒敘排列 返回陣列
}];