NSArray数组分组归类
[TOC]
数组基本用法
遍历数组
// for循环
NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil nil];
int count = array.count;
for (int i = 0; i<count; i++) { id obj = [array objectAtIndex:i]; }
// for in 快速遍历
NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil nil];
int i =0;
for (id obj in array) { NSLog(@"%i-%@", i, obj); i++; }
// 代码块遍历
NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
[array enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOLBOOL *stop) { NSLog(@"%i-%@", idx, obj);
// 如果索引为1,就停止遍历
if (idx == 1) {
// 利用指针修改外面BOOL变量的值
*stop = YES;
}
}];
数组排序
// 第一种
NSArray *array = [NSArray arrayWithObjects:@"2", @"3", @"1", @"4", nil nil];
// 返回一个排好序的数组,原来数组的元素顺序不会改变
// 指定元素的比较方法:compare:
NSArray *array1 = [array sortedArrayUsingSelector:@selector(compare:)]; NSLog(@"array1:%@", array1);
// 第二种
NSArray *sortDesc = @[[[NSSortDescriptor alloc] initWithKey:nil ascending:YES]];
NSArray *sortSetArray = [array sortedArrayUsingDescriptors:sortDesc];
// 第三种
NSArray *test = @[ 1.8T,2.0T,2.0T,3.0L,1.8T,1.8T,2.0T,3.0L,1.8T,2.0T,1.8T,1.8T, 2.0T,1.8T,2.0T,2.0T,3.2L,1.8T,1.8T,2.0T,2.0T,3.2L,2.0T,3.2L, ];
NSArray *array3 = [self getNewArray test];
// 返回一个排序好的数组
- (NSArray *)getNewArray:(NSArray *)array {
NSArray *arrayM = [array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
if (NSOrderedDescending == [obj1 compare:obj2 options:NSNumericSearch]) {
return (NSComparisonResult)NSOrderedDescending;
}
if (NSOrderedAscending==[obj1 compare:obj2 options:NSNumericSearch]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
return arrayM;
}
分组归类
使用场景 :根据NSAarry中NSDictionary的一个key值,将数组重新分组,下面是后台返回的数据:
(
{brand_id = 1; brand_name = 奥迪; initial = A;},
{brand_id = 2; brand_name = 阿斯顿·马丁; initial = A;},
{brand_id = 11; brand_name = 保时捷; initial = B;},
{brand_id = 7; brand_name = 宝马; initial = B;},
{brand_id = 24; brand_name = 昌河; initial = C;},
{brand_id = 22; brand_name = 长城; initial = C;}
)
代码实现:
NSArray *testArray = @[
@{@"brand_id": @"1", @"brand_name": @"奥迪", @"initial": @"A"},
@{@"brand_id": @"2", @"brand_name": @"阿斯顿·马丁", @"initial": @"A"},
@{@"brand_id": @"3", @"brand_name": @"保时捷", @"initial": @"B"},
@{@"brand_id": @"4", @"brand_name": @"宝马", @"initial": @"B"},
@{@"brand_id": @"4", @"brand_name": @"昌河", @"initial": @"C"},
@{@"brand_id": @"5", @"brand_name": @"长城", @"initial": @"C"} ];
// 获取数组中所用的initial的value值,indexArray数组中是乱序排列的。
NSMutableArray *indexArray = [NSMutableArray array];
indexArray = [testArray valueForKeyPath:@"initial"];
// 将array装换成NSSet类型,重新排序生成新的数组
NSSet *indexSet = [NSSet setWithArray:indexArray];
NSArray *sortDesc = @[[[NSSortDescriptor alloc] initWithKey:nil ascending:YES]];
NSArray *sortSetArray = [indexSet sortedArrayUsingDescriptors:sortDesc];
// 遍历数组并进行归类
NSMutableArray *resultArray = [NSMutableArray array];
[sortSetArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 根据NSPredicate获取array
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"initial == %@",obj]; NSArray *indexArray = [testArray filteredArrayUsingPredicate:predicate];
// 将查询结果加入到resultArray中
[resultArray addObject:indexArray];
}];
过滤重复元素和排序
// 最简洁,但返回的结果是无序的。
NSArray *arr = @[@"12-11", @"12-11", @"12-11", @"12-12", @"12-13", @"12-14"];
NSSet *set = [NSSet setWithArray:arr];
// 去除相同元素并重新排序
NSArray *arr = @[@"12-11", @"12-11", @"12-11", @"12-12", @"12-13", @"12-14"];
NSSet *set = [NSSet setWithArray:arr];
NSArray *sortDesc = @[[[NSSortDescriptor alloc] initWithKey:nil ascending:YES]];
NSArray *sortSetArray = [set sortedArrayUsingDescriptors:sortDesc];
// 利用NSDictionary的AllKeys(AllValues)方法
NSArray *dataArray =@[@"12-11", @"12-11", @"12-11", @"12-12", @"12-13", @"12-14"];
NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithCapacity:0];
for(NSString *str in dataArray){
[dic setValue:str forKey:str];
}
NSLog(@"%@",[dic allKeys]);
// 利用数组的containsObject来去除
NSArray *dataArray =@[@"12-11", @"12-11", @"12-11", @"12-12", @"12-13", @"12-14"];
NSMutableArray *listAry = [[NSMutableArray alloc]init];
for (NSString *str in dataArray) {
if (![listAry containsObject:str]) {
[listAry addObject:str];
}
}
NSLog(@"%@",listAry);