iOS 笔记-根据时间排序

根据时间排序

升序

NSSortDescriptor*sorter=[[NSSortDescriptor alloc]initWithKey:@"time" ascending:YES];

NSMutableArray *sortDescriptors=[[NSMutableArray alloc]initWithObjects:&sorter count:1];

NSArray *sortArray=[timeArray sortedArrayUsingDescriptors:sortDescriptors];



降序

NSArray *sortArray = [objectArray sortedArrayUsingComparator:^(FileModel *string1, FileModel *string2) {

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat: @"yyyy/MM/dd HH:mm"];

NSDate *date1= [dateFormatter dateFromString:string1.updatetime];

NSDate *date2= [dateFormatter dateFromString:string2.updatetime];

if (date1 == [date1 earlierDate: date2]) { //不使用intValue比较无效

return NSOrderedDescending;//降序

}else if (date1 == [date1 laterDate: date2]) {

return NSOrderedAscending;//升序

}else{

return NSOrderedSame;//相等

}

}];

你可能感兴趣的:(iOS 笔记-根据时间排序)