如何根据时间排序json数组?
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; //倒叙排列 返回数组
}];
网友评论