ios - How to order a NSarray with a dictionary by its values -
this question has answer here:
- sort nsmutabledictionary 6 answers
i have following nsarray
has nsdictionary
, need order them higher dictionary value goes on top.
i've researched around can not find predicate sort nsdictionary. how achieve objective??
nsarray* result = @[@{ @"chest":[nsstring stringwithformat:@"%i",[chestr count]]}, @{@"back":[nsstring stringwithformat:@"%i",[backr count]]}, @{@"triceps":[nsstring stringwithformat:@"%i",[tricepr count]]}, @{@"biceps":[nsstring stringwithformat:@"%i",[bicepr count]]}, @{@"arms-other":[nsstring stringwithformat:@"%i",[armsr count]]}, @{@"legs":[nsstring stringwithformat:@"%i",[legsr count]]}, @{@"shoulders":[nsstring stringwithformat:@"%i",[shouldersr count]]}, @{@"abs":[nsstring stringwithformat:@"%i",[absr count]]}, @{@"cardio":[nsstring stringwithformat:@"%i",[cardior count]]}, @{@"fitness":[nsstring stringwithformat:@"%i",[fitnessr count]]} ];
edit , solution
this how solved problem
nsarray* result = @[@{ @"chest":@"5"}, @{@"back":@"3"}, @{@"triceps":@"4"}, @{@"biceps":@"9"}, @{@"arms-other":@"1"}, @{@"legs":@"0"}, @{@"shoulders":@"2"}, @{@"abs":@"3"}, @{@"cardio":@"8"}, @{@"fitness":@"9"} ]; nsarray *sorted = [result sortedarrayusingcomparator:^(id obj1, id obj2){ nsarray *s1k = [obj1 allkeys]; nsinteger s1 = [obj1[[s1k objectatindex:0]] intvalue]; nsarray *s2k = [obj2 allkeys]; nsinteger s2 = [obj2[[s2k objectatindex:0]] intvalue]; if ( s1 > s2) { return (nscomparisonresult)nsorderedascending; } else if (s1 < s2) { return (nscomparisonresult)nsordereddescending; } return (nscomparisonresult)nsorderedsame; }];
a dictionary not sorted - definition.
you may convert array. nsdictionary instance method -allvalues may suitable doing so.
then can sort new array using custom sort descriptor ensures sorted in way want be.
Comments
Post a Comment