ios - How can i create array in ascending order when multiple Dictionary in Array -
i have 1 array multiple dictionary object keys date
, type
want arrange array in ascending order date
key.
my array :
( { date = "06 september 2016"; type = photo; }, { date = "06 september 2016"; type = video; }, { date = "07 september 2016"; type = photo; }, { date = "07 september 2016"; type = video; }, { date = "06 september 2016"; type = photo; }, { date = "06 september 2016"; type = photo; }, { date = "07 september 2016"; type = photo; }, { date = "01 october 2016"; type = photo; }, { date = "06 september 2016"; type = photo; } )
please me. how can arrange in ascending order date.
you need use sortedarrayusingcomparator
way sort date string. first convert date string nsdate
using nsdateformatter
, compare.
nsdateformatter *df = [[nsdateformatter alloc] init]; [df setdateformat:@"dd mmmm yyyy"]; nsarray *sortedarray = [yourarray sortedarrayusingcomparator:^nscomparisonresult(nsdictionary *obj1, nsdictionary *obj2) { nsdate *d1 = [df datefromstring:(nsstring*)[obj1 objectforkey:@"date"]]; nsdate *d2 = [df datefromstring:(nsstring*)[obj2 objectforkey:@"date"]]; return [d1 compare: d2]; }];
Comments
Post a Comment