iphone - How to compare an array of NSStrings containing Dates in ascending order -
i have array in contains list of dates represented strings:
nsmutablearray *objarray=[[nsmutablearray alloc]init]; [objarray addobject:@"18-01-2013 2:51"]; [objarray addobject:@"16-01-2013 5:31"]; [objarray addobject:@"15-01-2013 3:51"]; [objarray addobject:@"17-01-2013 4:41"]; [objarray addobject:@"03-02-2013 3:21"]; [objarray addobject:@"05-01-2013 3:01"];
please tell me how arrange array in ascending order using dates.
nsdateformatter *formatter = [[[nsdateformatter alloc] init] autorelease]; [formatter setdateformat:@"dd-mm-yyyy hh:mm"];
nscomparator comparedates = ^(id string1, id string2) { nsdate *date1 = [formatter datefromstring:string1]; nsdate *date2 = [formatter datefromstring:string2]; homecoming [date1 compare:date2]; }; nssortdescriptor * sortdesc = [[[nssortdescriptor alloc] initwithkey:@"self" ascending:no comparator:comparedates]autorelease]; [objarray sortusingdescriptors:[nsarray arraywithobject:sortdesc]];
by using code got exact output, give thanks
iphone ios objective-c nsmutablearray
No comments:
Post a Comment