nsmutablearray - 对 NSIndexPaths 数组进行排序

标签 nsmutablearray nsindexpath

我有一个 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 .此外,sectionNSInteger ,所以你不能调用它的方法。

如下修改您的代码以对 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/

相关文章:

iphone - 将一组新数据写入 Plist 而不是覆盖它

ios - UITextview 在文本中心对齐中编写,快速

ios - 如果您知道行,是否可以派生索引路径?

ios - 核心数据 : no index path for table cell being reused, 单元格消失

iphone - 替换对象索引 :withObject: not working when passing in a NSString object

objective-c - 如何避免 NSArray 中的重复元素并在 TableView 单元格中打印

ios - 当 UICollectionView 最初加载其数据时,我们如何获得可见的 Cell IndexPath

ios - 滚动完成时居中单元格 UITableView

iphone - NSMutableArray 到 NSDictionary iOS 3.1.3 问题

ios - 从 TableView Objective C 获取 IndexPath.Row