OC数组根据日期排序

来源:转载


//ascending是否升序


NSSortDescriptor*sorter=[[NSSortDescriptor alloc]initWithKey:timeStr ascending:NO];


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


NSArray *sortArray=[objectArray sortedArrayUsingDescriptors:sortDescriptors];


NSLog(@"排序好的数组:%@",sortArray);


NSMutableArray *dateArray = [NSMutableArray array];


for (DetailModel *item in sortArray) {


if (![item isKindOfClass:[DetailModel class]]) {


return nil;


}


//去除首尾空格


NSString *dateStr = [item.timeStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];


//截取从头部到空格之间的字符串


NSRange range = [dateStr rangeOfString:@" "];


NSString *str = [dateStr substringToIndex:range.location];


BOOL isContain = NO;


for (NSMutableArray *arrayItem in dateArray) {


DetailModel *firstModel = [arrayItem firstObject];


NSString *firstDateStr = [firstModel.timeStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];


NSRange spaceRange = [firstDateStr rangeOfString:@" "];


NSString *firstStr = [firstDateStr substringToIndex:spaceRange.location];


if ([firstStr isEqualToString:str]) {


isContain = YES;


[arrayItem addObject:item];


break;


}


}


if (!isContain) {


[dateArray addObject:[NSMutableArray arrayWithObject:item]];


}


}


NSLog(@"排序好的已经分组的数组:%@",dateArray);





分享给朋友:
------分隔线----------------------------
您可能感兴趣的文章:
随机阅读: