iOS自带的两个排序算法对比

对500多条消息数据排序 用如下耗时函数打印耗时...

NSDate* tmpStartData = [NSDate date];

doubledeltaTime = [[NSDatedate]timeIntervalSinceDate:tmpStartData];

NSLog(@"ssssssssssssss  cost time = %f", deltaTime);

```

//排序一

-(NSArray*)sortArrayWith:(NSArray*)arry{

NSArray* sortArry=[arry sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {

Message* model_1 = (Message*)obj1;

Message* model_2 = (Message*)obj2;

NSDate*date1 = [selfreadTimeStr:model_1.createTime];

NSDate*date2 = [selfreadTimeStr:model_2.createTime];

if( [date1timeIntervalSinceNow] < [date2timeIntervalSinceNow]) {

returnNSOrderedAscending;

}else{

returnNSOrderedDescending;

}

}];

returnsortArry;

}

耗时1.17秒 卡到爆炸

//排序二

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"createTime" ascending:YES];

NSArray *sortArray = [NSArray arrayWithObjects:descriptor,nil];

NSMutableArray* allMsgs = [allMessage sortedArrayUsingDescriptors:sortArray].mutableCopy;


耗时0.0004 流畅

总结: 第一种方法慎用

你可能感兴趣的:(iOS自带的两个排序算法对比)