我有一个 NSMutableArray
包含 NSIndexPath
对象,我想按它们的 row
对它们进行排序, 升序。
最短/最简单的方法是什么?
这是我尝试过的:
[self.selectedIndexPaths sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSIndexPath *indexPath1 = obj1;
NSIndexPath *indexPath2 = obj2;
return [@(indexPath1.section) compare:@(indexPath2.section)];
}];
最佳答案
你说你想按 row
排序,但你比较section
.此外,section
是 NSInteger
,所以你不能调用它的方法。
如下修改您的代码以对 row
进行排序:
[self.selectedIndexPaths sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSInteger r1 = [obj1 row];
NSInteger r2 = [obj2 row];
if (r1 > r2) {
return (NSComparisonResult)NSOrderedDescending;
}
if (r1 < r2) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
关于nsmutablearray - 对 NSIndexPaths 数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14928793/