有些时候,我们需要把数组倒序排列来满足需求,我们可以这么做:
//数组初始化最后必须以nil结尾,表示数组元素结束
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"item0",@"item1",@"item2",@"item3",nil];
//逆向遍历
NSEnumerator *enumerator = [array reverseObjectEnumerator];
array = (NSMutableArray*)[enumerator allObjects];
NSLog(@"%@",array);
更新:对象的排序
NSArray * arr2 = [arr sortedArrayUsingComparator:^NSComparisonResult(Model * obj1, Model * obj2) {
NSComparisonResult result = [obj1.distance compare:obj2.distance];
return result;
}];