how to change order of NSMutable array in same way another mutable arrays get changed

后端 未结 5 822
遥遥无期
遥遥无期 2021-01-17 05:23

I have three arrays. They are name, birthdates and remaining days like below:

    name                         birth         


        
5条回答
  •  清酒与你
    2021-01-17 06:16

    such as your MutableArray is a Dictionarys array , you can use sortUsingComparator to sort the array

    [array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    
        int a = [(NSNumber *)[(NSDictionary *)obj1 objectiveForKey: @"remanning"] intValue];
        int b = [(NSNumber *)[(NSDictionary *)obj2 objectiveForKey: @"remanning"] intValue];
    
        if (a < b) {
            return NSOrderedAscending;
        }
        else if(a == b)
        {
            return NSOrderedSame;
        }
        return NSOrderedDescending;
    }];
    

    For example , I have a test :

    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@(30),@(20),@(5),@(100), nil];
    
    [array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    
        int a = [(NSNumber *)obj1 intValue];
        int b = [(NSNumber *)obj2 intValue];
    
        if (a < b) {
            return NSOrderedAscending;
        }
        else if(a == b)
        {
            return NSOrderedSame;
        }
        return NSOrderedDescending;
    }];
    
    [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"%@ , %d",obj,idx);
    }];
    

    then the output is : enter image description here

提交回复
热议问题