说一下标题意思, 就是一个数组中很多对象,每个对象都有相同属性,然后,属性值也相等的 分别放到一个数组,怎么实现呢
。。。。。
方式一
- (void)sameArrayGroup
{
MOBLabel *lab1 = [[MOBLabel alloc] init];
lab1.name = @"2";
MOBLabel *lab2 = [[MOBLabel alloc] init];
lab2.name = @"3";
MOBLabel *lab3 = [[MOBLabel alloc] init];
lab3.name = @"2";
MOBLabel *lab4 = [[MOBLabel alloc] init];
lab4.name = @"0";
MOBLabel *lab5 = [[MOBLabel alloc] init];
lab5.name = @"0";
NSArray *array2 = @[lab1, lab2, lab3, lab4, lab5];
NSMutableArray *array = [NSMutableArray arrayWithArray:array2];
NSMutableArray *dateMutablearray = [@[] mutableCopy];
for (int i = 0; i < array.count; i ++) {
MOBLabel *lab = array[i];
NSMutableArray *tempArray = [@[] mutableCopy];
[tempArray addObject:lab];
NSLog(@"====0000======");
for (int j = i+1; j < array.count; j ++) {
MOBLabel *jlab = array[j];
if([lab.name isEqualToString:jlab.name]){
[tempArray addObject:lab];
NSLog(@"====11====");
[array removeObjectAtIndex:j];
j -= 1;
}
}
[dateMutablearray addObject:tempArray];
}
NSLog(@"dateMutable:%@",dateMutablearray);
}
结果
dateMutable:(
(
">",
">"
),
(
">"
),
(
">",
">"
)
)
方式二
NSArray *array1 = @[@"2016-10-01",@"2016-10-02",@"2016-10-03",
@"2016-10-01",@"2016-10-02",@"2016-10-03",
@"2016-10-01",@"2016-10-02",@"2016-10-03",
@"2016-10-01",@"2016-10-02",@"2016-10-03",
@"2016-10-01",@"2016-10-02",@"2016-10-03",
@"2016-10-01",@"2016-10-02",@"2016-10-03",
@"2016-10-04",@"2016-10-06",@"2016-10-08",
@"2016-10-05",@"2016-10-07",@"2016-10-09"];
//NSArray *array2 = @[lab1, lab2, lab3, lab4, lab5];
NSMutableArray *array = [NSMutableArray arrayWithArray:array1];
NSMutableArray *dateMutablearray = [@[] mutableCopy];
for (int i = 0; i < array.count; i ++) {
NSString *string = array[i];
NSMutableArray *tempArray = [@[] mutableCopy];
[tempArray addObject:string];
for (int j = i+1; j < array.count; j ++) {
NSString *jstring = array[j];
if([string isEqualToString:jstring]){
[tempArray addObject:jstring];
[array removeObjectAtIndex:j];
j -= 1;
}
}
[dateMutablearray addObject:tempArray];
}
NSLog(@"dateMutable:%@",dateMutablearray);
结果
2018-04-12 15:38:08.641319+0800 MOBTestVC[11909:323618] dateMutable:(
(
"2016-10-01",
"2016-10-01",
"2016-10-01",
"2016-10-01",
"2016-10-01",
"2016-10-01"
),
(
"2016-10-02",
"2016-10-02",
"2016-10-02",
"2016-10-02",
"2016-10-02",
"2016-10-02"
),
(
"2016-10-03",
"2016-10-03",
"2016-10-03",
"2016-10-03",
"2016-10-03",
"2016-10-03"
),
(
"2016-10-04"
),
(
"2016-10-06"
),
(
"2016-10-08"
),
(
"2016-10-05"
),
(
"2016-10-07"
),
(
"2016-10-09"
)
)
比较简洁的方式======================
MOBLabel *lab1 = [[MOBLabel alloc] init];
lab1.name = @"2";
MOBLabel *lab2 = [[MOBLabel alloc] init];
lab2.name = @"3";
MOBLabel *lab3 = [[MOBLabel alloc] init];
lab3.name = @"2";
MOBLabel *lab4 = [[MOBLabel alloc] init];
lab4.name = @"0";
MOBLabel *lab5 = [[MOBLabel alloc] init];
lab5.name = @"0";
NSArray *array2 = @[lab1, lab2, lab3, lab4, lab5];
NSMutableDictionary *res = @{}.mutableCopy;
for (MOBLabel *obj in array2)
{
if (res[obj.name])
{
[res[obj.name] addObject:obj];
}
else
{
res[obj.name] = [NSMutableArray arrayWithObject:obj];
}
}
NSLog(@"-----%@",res.allValues);
结果
-----(
(
">",
">"
),
(
">"
),
(
">",
">"
)
)
```