iOS 各种排序
2017-08-03 本文已影响229人
秀才不才
数组排序
[array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2]; //升序
return [obj2 compare:obj1]; //降序
}];
数组中字典排序
NSMutableArray *tempArr = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if([[obj1 objectForKey:@"key"]intValue] < [[obj2 objectForKey:@"key"]intValue]){
return NSOrderedAscending;
}
if([[obj1 objectForKey:@"key"]intValue] > [[obj2 objectForKey:@"key"]intValue]){
return NSOrderedDescending;
}
return NSOrderedSame;
}];
数组中字典按照某个value排序
NSArray *sortArr = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"key" ascending:YES]];
[listArr sortUsingDescriptors:sortArr];
排序方法
[listArr sortUsingDescriptors:(nonnull NSArray<NSSortDescriptor *> *)];
[listArr sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {}];
[listArr sortUsingFunction:(nonnull NSInteger (*)(id _Nonnull __strong, id _Nonnull __strong, void * _Nullable)) context:(nullable void *)];
[listArr sortedArrayHint];
[listArr sortedArrayUsingSelector:(nonnull SEL)];
[listArr sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {}];
[listArr sortedArrayUsingFunction:(nonnull NSInteger (*)(id _Nonnull __strong, id _Nonnull __strong, void * _Nullable)) context:(nullable void *) hint:(nullable NSData *)];
[listArr sortedArrayWithOptions:(NSSortOptions) usingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {}];
[listArr sortWithOptions:(NSSortOptions) usingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {}];