ios - Reorganize dictionary order by values? -
i have app receives json file webserver , convert nsdictionary can see below:
[nsjsonserialization jsonobjectwithdata:data options:0 error:&jsonerror]
the structure of dictionary follows:
[0] => { "name" = "josh", meters = "8" } [1] => { "name" = "lina", meters = "10" } [2] => { "name" = "pall", meters = "21" }
you can see data organized key meters
, unfortunately webserver not give possibility maintain open connection, in case, new records come through apns (push notification)
assuming comes following record notification:
{"name" = "oli", meters = "12"}
i insert in dictionary, once that's done, how can rearrange array , order meters?
there's no order inherent in dictionary. see ordering in initial set accidental.
arrays have ordering, , can array dictionary sending allkeys
or allvalues
.
using fact organize dictionary, might try this:
nsarray *orderedpairs = [@[] mutablecopy]; (id key in [somedictionary allkeys]) { [orderedpairs addobject:@[key, somedictionary[key]]]; } [orderedpairs sortusingcomparator:^nscomparisonresult(nsarray *a, nsarray *b) { // return [a[0] compare:b[0]]; // sort key return [a[1] compare:b[1]]; // sort value }];
note result in array -- not dictionary -- 1 ordered.
Comments
Post a Comment