简体   繁体   English

NSArray对象与自定义逻辑排序

[英]NSArray of Objects sorting with Custom Logic

I have NSSArray having Custom Objects, To sort the Custom Object with Custom logic 我有具有自定义对象的NSSArray ,使用自定义逻辑对自定义对象进行排序

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

if isValidName property is false, Then the custom object should be last sorting order. 如果isValidName属性为false,则自定义对象应为最后的排序顺序。 for the objects isValidName property is true, Then it should be sort with property objectName in ascending. 因为对象的isValidName属性为true,则应按升序对属性objectName进行排序。

I can do with for loop by finding objects having property isValidName false then remove the objects from the array and then sort the array after that I can add the objects which are having property isValidName false. 我可以做的for loop通过寻找具有对象属性isValidName假,那么从数组中删除的对象,然后在后阵,我可以添加它们是具有属性的对象isValidName假排序。 I don't want to do that. 我不想那样做。

I want that something like below option, But it is not working. 我想要类似下面的选项,但是它不起作用。 Please help in regard 请帮忙

[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;

    }];

I dont really recreated the stuff you did, but i hope this would work for you: arr should be NSMutableArray 我并没有真正重新创建您所做的工作,但是我希望这对您有用:arr应该是NSMutableArray

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

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

So if your object.isValidName = false it will push to after the one have it true and also sort by name 因此,如果您的object.isValidName = false,它将在推入true并按名称排序后推送到

Try using NSPredicate 尝试使用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);

I think you almost had it, but you shouldn't be using enumerateObjectsUsingBlock . 我认为您几乎已经拥有了,但是您不应该使用enumerateObjectsUsingBlock Instead use sortedArrayUsingComparator and assign the result to a new array: 而是使用sortedArrayUsingComparator并将结果分配给新数组:

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

If you have an NSMutableArray you can call sortUsingComparator: on it instead and avoid creating a new array. 如果您有NSMutableArray ,则可以在其上调用sortUsingComparator:避免创建新数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM