0

我有NSArray一些类似以下格式的东西。

组数组是:

(
    "Q-1-A1",
    "Q-1-A9",
    "Q-2-A1",
    "Q-2-A5",
    "Q-3-A1",
    "Q-3-A8",
    "Q-4-A1",
    "Q-4-A4",
    "Q-10-A2",
    "Q-8-A2",
    "Q-9-A2",
    "Q-7-A1",
    "Q-5-A2"
)

现在我要做的就是对数组元素进行分组,就像这样。

1 = ( "Q-1-A1","Q-1-A9")
2 = ("Q-2-A1","Q-2-A5",) ...
10 =("Q-10-A2")

任何人都可以帮助我如何实现这一目标。

提前致谢。

4

4 回答 4

6

尝试

NSArray *array = @[@"Q-1-A1",
                   @"Q-1-A9",
                   @"Q-2-A1",
                   @"Q-2-A5",
                   @"Q-3-A1",
                   @"Q-3-A8",
                   @"Q-4-A1",
                   @"Q-4-A4",
                   @"Q-10-A2",
                   @"Q-8-A2",
                   @"Q-9-A2",
                   @"Q-7-A1",
                   @"Q-5-A2"];

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

for (NSString *string in array) {

    NSArray *components = [string componentsSeparatedByString:@"-"];

    NSString *key = components[1];
    NSMutableArray *tempArray = dictionary[key];
    if (!tempArray) {
        tempArray = [NSMutableArray array];
    }
    [tempArray addObject:string];
    dictionary[key] = tempArray;
}
于 2013-06-18T11:37:06.247 回答
2

创建一个 NSMutableDictionary,然后遍历您的“组数组”。

对于每个 NSString 对象:

  1. 获取 componentsSeparatedByString 的 NSArray:@"-"

  2. 使用第二个组件创建一个键并从可变字典中检索该键的对象。如果它为 nil,则将其设置为一个空的 NSMutableArray。

  3. 将原始 NSString 添加到可变数组。

于 2013-06-18T11:32:00.143 回答
1

试试这个

NSArray *arrData =[[NSArray alloc]initWithObjects:@"Q-1-A1",@"Q-1-A9",@"Q-2-A1",@"Q-2-A5",@"Q-3-A1",@"Q-3-A8",@"Q-4-A1",@"Q-4-A4",@"Q-10-A2",@"Q-8-A2",@"Q-9-A2",@"Q-7-A1",@"Q-5-A2", nil ];
NSMutableDictionary *dictList = [[NSMutableDictionary alloc]init];

for (int i=0; i<[arrData count];i++) {
    NSArray *arrItem = [[arrData objectAtIndex:i] componentsSeparatedByString:@"-"];
    NSMutableArray *arrSplitedItems = [dictList valueForKey:[arrItem objectAtIndex:1]];
    if (!arrSplitedItems) {
        arrSplitedItems = [NSMutableArray array];
    }
    [arrSplitedItems addObject:[arrData objectAtIndex:i]];
    [dictList setValue:arrSplitedItems forKey:[arrItem objectAtIndex:1]];
}
NSArray *sortedKeys =[dictList allKeys];
NSArray *sortedArray = [sortedKeys sortedArrayUsingComparator:^(id str1, id str2) {
    return [((NSString *)str1) compare:((NSString *)str2) options:NSNumericSearch];
}];
for (int i=0; i<[sortedArray count]; i++) {
    NSLog(@"%@",[dictList objectForKey:[sortedArray objectAtIndex:i]]);
}
于 2013-06-18T13:12:28.033 回答
0
listOfYourMainArray/// Its YOur main Array;
temArray = (NSArray *)listOfYourMainArray; // Add Your main array to `temArray`.
NSMutableDictionary *lastDic = [[NSMutableDictionary alloc] init]; /// YOu need to creat Dictionary for arrange your values.

for (int i = 0; i< listOfYourMainArray.count; i++)
{
   for (int j = 0 ; j < temArray.count; j ++)
   {
     if (([[temArray objectAtIndex:j] rangeOfString:[NSString stringWithFormat:@"Q-%d", i] options:NSCaseInsensitiveSearch].location != NSNotFound))
     {
         [lastDic setValue:[temArray objectAtIndex:j] forKey:[NSString stringWithFormat:@"%d", i]];
     }
   }  
}

NSLog(@"%@", lastDic)
于 2013-06-18T11:33:23.977 回答