iphone - Objective-C Sort String Date Array -
i have array consists of dates nsstring
. how sort descending?
edit : ended tweaking code use nsdate using other methods won't work in case
you use sortedarrayusingfunction
, consider bellow example
nsstring *str1 = @"03-07-2012"; nsstring *str2 = @"01-07-2012"; nsstring *str3 = @"02-07-2012"; nsarray *arr = [nsarray arraywithobjects:str1, str2, str3, nil]; arr = [arr sortedarrayusingfunction:datesort context:nil]; //the date sort function nscomparisonresult datesort(nsstring *s1, nsstring *s2, void *context) { nsdateformatter *formatter = [[nsdateformatter alloc] init]; [formatter setdateformat:@"dd-mm-yyyy"]; nsdate *d1 = [formatter datefromstring:s1]; nsdate *d2 = [formatter datefromstring:s2]; return [d1 compare:d2]; // ascending order return [d2 compare:d1]; // descending order }
Comments
Post a Comment