将数组排序到字典中

时间:2011-08-07 15:08:24

标签: iphone ios arrays sorting nsdictionary

我有很多字符串的数组。 我不想把它们排成字典,所以所有字符串都是从同一个字母开始进入一个数组然后数组成为一个键的值;键将是其值的数组中的所有单词开始的字母。

实施例

Key = "A" >> Value = "array = apple, animal, alphabet, abc ..."
Key = "B" >> Value = "array = bat, ball, banana ..."

我该怎么做? 非常感谢提前!

5 个答案:

答案 0 :(得分:15)

NSArray *list = [NSArray arrayWithObjects:@"apple, animal, bat, ball", nil];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSString *word in list) {
    NSString *firstLetter = [[word substringToIndex:1] uppercaseString];
    NSMutableArray *letterList = [dict objectForKey:firstLetter];
    if (!letterList) {
        letterList = [NSMutableArray array];
        [dict setObject:letterList forKey:firstLetter];
    }
    [letterList addObject:word];
}
NSLog(@"%@", dict);

答案 1 :(得分:1)

您可以通过以下步骤实现您想要的目标:

  1. 创建一个空但可变的字典。
  2. 获取第一个角色。
  3. 如果该角色的密钥不存在,请创建它。
  4. 将单词添加到键的值(应该是NSMutableArray)。
  5. 对所有按键重复步骤#2。
  6. 以下是这些步骤的Objective-C代码。请注意,我假设您希望密钥为不敏感

    // create our dummy dataset
    NSArray * wordArray = [NSArray arrayWithObjects:@"Apple", 
                           @"Pickle", @"Monkey", @"Taco", 
                           @"arsenal", @"punch", @"twitch", 
                           @"mushy", nil];
    // setup a dictionary
    NSMutableDictionary * wordDictionary = [[NSMutableDictionary alloc] init];
    for (NSString * word in wordArray) {
        // remove uppercaseString if you wish to keys case sensitive.
        NSString * letter = [[word substringWithRange:NSMakeRange(0, 1)] uppercaseString];
        NSMutableArray * array = [wordDictionary objectForKey:letter];
        if (!array) {
            // the key doesn't exist, so we will create it.
            [wordDictionary setObject:(array = [NSMutableArray array]) forKey:letter];
        }
        [array addObject:word];
    }
    NSLog(@"Word dictionary: %@", wordDictionary);
    

答案 2 :(得分:0)

看看这个主题,它们解决了与你几乎相同的问题 - filtering NSArray into a new NSArray in objective-c如果它没有用,请告诉我,所以我会再为你写一个代码示例。

答案 3 :(得分:0)

使用此按字母顺序对数组内容进行排序,进一步设计为需求

[keywordListArr sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

答案 4 :(得分:-1)

我刚刚写了这个样本。它看起来很简单,可以满足您的需求。

NSArray *names = [NSArray arrayWithObjects:@"Anna", @"Antony", @"Jack", @"John", @"Nikita", @"Mark", @"Matthew", nil];

NSString *alphabet = @"ABCDEFGHIJKLMNOPQRSTUWXYZ";
NSMutableDictionary *sortedNames = [NSMutableDictionary dictionary];

for(int characterIndex = 0; characterIndex < 25; characterIndex++) {
    NSString *alphabetCharacter = [alphabet substringWithRange:NSMakeRange(characterIndex, 1)];
    NSArray *filteredNames = [names filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF BEGINSWITH[C] %@", alphabetCharacter]];        
    [sortedNames setObject:filteredNames forKey:alphabetCharacter];
}

//Just for testing purposes let's take a look into our sorted data
for(NSString *key in sortedNames) {
    for(NSString *value in [sortedNames valueForKey:key]) {
        NSLog(@"%@:%@", key, value);
    }
}