-1

Possible Duplicate:
How can I sort an NSArray containing NSDictionaries?

I have an array of dictionaries.. This is what my dictionary looks like

MOD = 0;
MAU= 30;
MODN = "SOME WORD";
MODID = 518;

I have tried sorting the array of dictionaries like this

sortedArray = [[ICArray valueForKey:@"MODN"] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

However this only gives me an array of the values in MODN in alphabetical order... where in actual fact I am hoping to get the each dictionary sorted into an array.

Any help would be greatly appreciated..

2

1 Answer 1

0

You can use a custom block based comparator to look at the values in each of the dictionaries, and sort however you want. Something like below should work for you

NSArray *unsortedArray = @[@{@"MOD" : @(0), @"MAU" : @(30), @"MODN" : @"SOME WORD", @"MODID" : @(518)},
                           @{@"MOD" : @(0), @"MAU" : @(30), @"MODN" : @"ANOTHER WORD", @"MODID" : @(518)},
                           @{@"MOD" : @(0), @"MAU" : @(30), @"MODN" : @"THIRD WORD", @"MODID" : @(518)}];

NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(id obj1, id obj2) {
  return [[obj1 valueForKey:@"MODN"] localizedCaseInsensitiveCompare:[obj2 valueForKey:@"MODN"]];
}];
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.