Henrik LP
Henrik LP

Reputation: 159

How can I alphabetically sort the values in my NSDictionary with an NSArray?

I have an NSDictionary which contains several arrays with several strings in them. I want to put all strings under each array in one single array. How can I receive all the strings at once? I've tried this:

NSMutableArray *mutarr = [[NSMutableArray alloc] initWithObjects:[self.firstTableView allValues], nil];
NSArray *mySorted = [[NSArray alloc]init];
mySorted = [mutarr sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

NSLog(@"First table view values: %@", mySorted);

NOTE: self.firsttableview is a NSDictionary like this:

NSString *path = [[NSBundle mainBundle] pathForResource:@"firstTableView" ofType:@"plist"];

self.firstTableView = [NSDictionary dictionaryWithContentsOfFile:path];

EFFECT: This gives me a list in NSLog, but it isn't in alphabetic order.

Upvotes: 0

Views: 1044

Answers (3)

Patrick Goley
Patrick Goley

Reputation: 5417

You are initializing your mutarr with one object which is the array returned by allValues on your dictionary. Instead try this:

NSMutableArray* mutarr = [NSMutableArray array];

for (NSArray* array in self.firstTableView.allValues) {
    [mutarr addObjectsFromArray:array];
}

Upvotes: 2

ahmedalkaff
ahmedalkaff

Reputation: 313

If your NSDictionary values are arrays then you are trying to sort the arrays instead of the strings. Try to create a for loop to iterate [self.firstTableView allValues] then add all values in that array to your main to sort array.

Upvotes: 0

Wain
Wain

Reputation: 119041

Your code is going to mean that mutarr is an array containing an array of arrays, not an array of strings. You should be looping over the array values from the dictionary and adding the items from each one to your mutarr to make it an array of strings that you can sort.

Upvotes: 0

Related Questions