参考:https://www.jianshu.com/p/732461f2439a
[arr sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
BTItemModel *item1 = (BTItemModel *)obj1;
BTItemModel *item2 = (BTItemModel *)obj2;
return [item1.rank compare:item2.rank];
}];
错误方式,结果是无序的:
[arr sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
BTItemModel *item1 = (BTItemModel *)obj1;
BTItemModel *item2 = (BTItemModel *)obj2;
if (item1.rank > item2.rank) {
return NSOrderedDescending;
} else {
return NSOrderedAscending;
}
return 0;
}];
规范方式:
NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
原文地址:https://www.cnblogs.com/huaida/p/12160806.html
时间: 2024-10-16 01:42:54