简体   繁体   English

将数组排序到字典中

[英]Sort array into dictionary

I have and array of many strings. 我有很多字符串的数组。 I wan't to sort them into a dictionary, so all strings starting the same letter go into one array and then the array becomes the value for a key; 我不想把它们排成字典,所以所有字符串都是从同一个字母开始进入一个数组然后数组成为一个键的值; the key would be the letter with which all the words in it's value's array begin. 键将是其值的数组中的所有单词开始的字母。

Example

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

How can I do that? 我怎样才能做到这一点? Thanks a lot in advance! 非常感谢提前!

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);

You can achieve what you want through the following steps: 您可以通过以下步骤实现您的目标:

  1. Create an empty but mutable dictionary. 创建一个空但可变的字典。
  2. Get the first character. 获得第一个角色。
  3. If a key for that character does not exist, create it. 如果该角色的密钥不存在,请创建它。
  4. Add the word to the value of the key (should be an NSMutableArray). 将单词添加到键的值(应该是NSMutableArray)。
  5. Repeat step #2 for all keys. 对所有键重复步骤#2。

Here is the Objective-C code for these steps. 以下是这些步骤的Objective-C代码。 Note that I am assuming that you want the keys to be case insensitive . 请注意,我假设您希望密钥不区分大小写

// 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);

看一下这个主题,它们解决了几乎和你一样的问题 - 在目标中将NSArray过滤成新的NSArray-c让我知道它是否有用,所以我会再为你编写一个代码示例。

Use this to sort the contents of array in alphabetical order, further you design to the requirement 使用它按字母顺序对数组的内容进行排序,进一步设计符合要求

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

I just wrote this sample. 我刚刚写了这个样本。 It looks simple and does what you need. 它看起来很简单,可以满足您的需求。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM