ios - Sort values of NSMutableDictionary alphabetically -
i have nsmutabledictionary integers key , nsstring values example:
key -- value
1 -- b
2 -- c
3 -- a
now want sort nsmutabledictionary alphabetically using values. want dictionary : key(3,1,2)->value(a,b,c). how can perform this?
from apple docs: https://developer.apple.com/library/mac/documentation/cocoa/conceptual/collections/articles/dictionaries.html#//apple_ref/doc/uid/20000134-sw4
sorting dictionary keys value:
nsdictionary *dict = [nsdictionary dictionarywithobjectsandkeys: [nsnumber numberwithint:63], @"mathematics", [nsnumber numberwithint:72], @"english", [nsnumber numberwithint:55], @"history", [nsnumber numberwithint:49], @"geography", nil]; nsarray *sortedkeysarray = [dict keyssortedbyvalueusingselector:@selector(compare:)]; // sortedkeysarray contains: geography, history, mathematics, english
blocks ease custom sorting of dictionaries:
nsarray *blocksortedkeys = [dict keyssortedbyvalueusingcomparator: ^(id obj1, id obj2) { if ([obj1 integervalue] > [obj2 integervalue]) { return (nscomparisonresult)nsordereddescending; } if ([obj1 integervalue] < [obj2 integervalue]) { return (nscomparisonresult)nsorderedascending; } return (nscomparisonresult)nsorderedsame; }];
Comments
Post a Comment