Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 702
Martin Herman Avatar asked Aug 07 '11 15:08

Martin Herman


People also ask

How do you sort an array in a dictionary?

To sort a list of dictionaries according to the value of the specific key, specify the key parameter of the sort() method or the sorted() function. By specifying a function to be applied to each element of the list, it is sorted according to the result of that function.

Can you sort values in a dictionary?

It is not possible to sort a dictionary, only to get a representation of a dictionary that is sorted. Dictionaries are inherently orderless, but other types, such as lists and tuples, are not. So you need an ordered data type to represent sorted values, which will be a list—probably a list of tuples.

How do you sort an array by key in Python?

To sort a dictionary by value in Python you can use the sorted() function. Python's sorted() function can be used to sort dictionaries by key, which allows for a custom sorting method. sorted() takes three arguments: object, key, and reverse.


2 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);
like image 84
omz Avatar answered Sep 27 '22 20:09

omz


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).
  5. Repeat step #2 for all keys.

Here is the Objective-C code for these steps. 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);
like image 44
Alex Nichol Avatar answered Sep 27 '22 22:09

Alex Nichol