使用自定义逻辑排序的对象的 NSArray

NSArray of Objects sorting with Custom Logic

NSSArray 有自定义对象,要使用自定义逻辑对自定义对象进行排序

自定义对象有两个 属性:
@property (nonatomic, strong) NSString *objectName;
@property (nonatomic, assign) BOOL isValidName;

如果 isValidName 属性 为假,那么自定义对象应该是最后的排序顺序。对于objects isValidName 属性 为true,那么应该按照属性 objectName 升序排序。

我可以通过查找具有 属性 isValidName false 的对象来处理 for loop,然后从数组中删除对象,然后对数组进行排序,然后我可以添加具有 [=27 的对象=] isValidName 假。我不想那样做。

我想要类似下面的选项,但它不起作用。请帮忙

[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSArray *sortedTopics = [obj sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
             if (obj1.isValidName) {  
                return NSOrderedAscending;
            }
            return [obj1.objectName compare: obj2.objectName options:NSCaseInsensitiveSearch];
        }];
        obj = sortedTopics;

    }];

我并没有真正重新创建你所做的东西,但我希望这对你有用:arr 应该是 NSMutableArray

NSSortDescriptor *validSort = [[NSSortDescriptor alloc] initWithKey:@"isValidName" ascending:NO];
NSSortDescriptor *nameSort = [[NSSortDescriptor alloc] initWithKey:@"objectName" ascending:YES];

[arr sortUsingDescriptors:[NSArray validSort, nameSort, nil]];

因此,如果您的 object.isValidName = false 它将推送到一个为真之后,并按名称排序

尝试使用 NSPredicate

  NSMutableArray *arrFinalSortedArray = [NSMutableArray new];

NSArray *yourObjectArray = @[@{@"isValidName":@"YES" ,@"objectName":@"ABC"},@{@"isValidName":@"NO",@"objectName":@"EFG"},@{@"isValidName":@"YES",@"objectName":@"BCD"},@{@"isValidName":@"YES",@"objectName":@"CDE"},@{@"isValidName":@"NO",@"objectName":@"DEF"}];

    // filter for NO flag
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isValidName ==[c] %@", @"NO"];
    NSArray *NOContainerArray = [yourObjectArray  filteredArrayUsingPredicate:predicate];
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"objectName" ascending:YES];
    NOContainerArray = [NOContainerArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];

    // filter for NO flag
    predicate = [NSPredicate predicateWithFormat:@"isValidName ==[c] %@", @"YES"];
    NSArray *YESContainerArray = [yourObjectArray  filteredArrayUsingPredicate:predicate];
    descriptor = [NSSortDescriptor sortDescriptorWithKey:@"objectName" ascending:YES];
    YESContainerArray = [YESContainerArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];

    // add to all YES and NO array into the final array which is the your result
    [arrFinalSortedArray addObjectsFromArray:YESContainerArray];
    [arrFinalSortedArray addObjectsFromArray:NOContainerArray];
    NSLog(@"%@",arrFinalSortedArray);

我想你几乎已经掌握了它,但你不应该使用 enumerateObjectsUsingBlock。而是使用 sortedArrayUsingComparator 并将结果分配给一个新数组:

NSArray *sortedTopics = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
    if (!obj1.isValidName)
        return NSOrderedDescending;
    return [obj1.objectName compare: obj2.objectName options:NSCaseInsensitiveSearch];
}];

如果您有一个 NSMutableArray,您可以在其上调用 sortUsingComparator:,避免创建新数组。