removeObjectsInArray隐藏细节 数组排序

来源:互联网 发布:2016十大网络歌曲 编辑:程序博客网 时间:2024/05/01 19:00


NSMutableArray *a = [[NSMutableArray alloc] 

initWithObjects:@"1",@"3",@"5",@"1", nil];

NSMutableArray *b = [[NSMutableArray alloc] 

initWithObjects:@"1", nil];

[a removeObjectsInArray:b];


这时候数组a就变成了 {@“3”,@“5”,nil};

而不是{@“3”,@“5”,@“1”,nil};

也不是{@“1”,@“3”,@“5”,nil};


数组排序:

NSComparator cmptr = ^(id obj1, id obj2){

                if ([obj1 integerValue] > [obj2 integerValue]) {

                    return (NSComparisonResult)NSOrderedAscending;

                }

                

                if ([obj1 integerValue] < [obj2 integerValue]) {

                    return (NSComparisonResult)NSOrderedDescending;

                }

                return (NSComparisonResult)NSOrderedSame;

            };

            

[a sortUsingComparator:cmptr];


结果:

[@"5",@"3",@"1",@"1"nil];
原创粉丝点击