Sort an array into a dictionary

I have an array of several lines. I won’t sort them in the dictionary, so all lines starting with the same letter fall into one array, and then the array becomes the value for the key; the key will be the letter with which all words in this array of values ​​begin.

Example

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

How can i do this? Thank you very much in advance!

+6
source share
5 answers
 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); 
+15
source

You can achieve what you want by following these steps:

  • Create an empty but mutable dictionary.
  • Get the first character.
  • If the key for this symbol does not exist, create it.
  • Add the word to the key value (must be NSMutableArray).
  • Repeat step # 2 for all keys.

Here is the Objective-C code for these steps. Please note that I assume that you want the keys to be 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); 
+1
source

Take a look at this topic, they solve almost the same problem as you - filtering NSArray into a new NSArray in objective-c . Let me know if it doesn’t help, so I will write another example code for you.

0
source

Use this to sort the contents of the array in alphabetical order, then you create a requirement

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

0
source

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 take a look into our sorted data for(NSString *key in sortedNames) { for(NSString *value in [sortedNames valueForKey:key]) { NSLog(@"%@:%@", key, value); } } 
-1
source

Source: https://habr.com/ru/post/894487/


All Articles