Sorting NSMutableArray using SortDescriptor AND Predicate possible?

前端 未结 1 1244
太阳男子
太阳男子 2021-01-01 03:29

I have an array of type \"Restaurant\" which has an NSSet of \"Rating.\" Rating has an ID and a value.

I want to sort the array of Restaurant\'s by rating with an ID

相关标签:
1条回答
  • 2021-01-01 04:16

    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;
    }];
    
    0 讨论(0)
提交回复
热议问题