-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..

Community
  • 1
  • 1
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183
  • Have a look at http://stackoverflow.com/questions/8483172/ios-sort-array-with-dictionaries and http://stackoverflow.com/questions/2149069/iphone-obj-c-sorting-a-mutable-array-of-dictionaries-display-a-string-but – user427969 Nov 02 '12 at 01:19
  • thanks for the suggestions, I dont think the second version is a very good solution for what I am tying to achieve.. The first one is good but just trying to get it to work for my solution now.... – HurkNburkS Nov 02 '12 at 01:33

1 Answers1

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"]];
}];
rickerbh
  • 9,731
  • 1
  • 31
  • 35