Sorting NSMutableArray using SortDescriptor AND Predicate possible?

こ雲淡風輕ζ 提交于 2019-11-30 10:01:57

You can sort the array using a NSComparator block.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"rating_id = %d", ratingID];

NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

    int value1 = [[[[obj1 ratings] filteredSetUsingPredicate:predicate] anyObject] currentValue];
    int value2 = [[[[obj2 ratings] filteredSetUsingPredicate:predicate] anyObject] currentValue];

    if ( value1 < value2 )
        return NSOrderedAscending;
    if ( value1 > value2 )
        return NSOrderedDescending;
    return NSOrderedSame;
}];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!