1.不变数组排序
//升序排序 //看小于号返回什么就是什么排序
-(NSArray *)orderedTimeWithArray:(NSArray *)array
{
NSArray *arr = [array sortedArrayUsingComparator:^NSComparisonResult(Pre_Message *obj1, Pre_Message *obj2) {
double time1 = [obj1.time doubleValue];
double time2 = [obj2.time doubleValue];
if (time1 < time2) {
return NSOrderedAscending; //升序
}
else if (time1 == time2)
{
return NSOrderedSame;
}
else
{
return NSOrderedDescending;
}
}];
return arr;
}
2.可变数组排序
//升序排序 //看小于号返回什么就是什么排序
-(NSMutableArray *)orderedTimeWithArray:(NSMutableArray *)array
{
[array sortUsingComparator:^NSComparisonResult(Pre_Message *obj1, Pre_Message *obj2) {
double time1 = [obj1.time doubleValue];
double time2 = [obj2.time doubleValue];
if (time1 < time2) {
return NSOrderedAscending;
}
else if (time1 == time2)
{
return NSOrderedSame;
}
else
{
return NSOrderedDescending;
}
}];
return array;
}
网友评论