objective c - Sorting NSMutablearray by NSString as a date -
i have array, filled out string objects. inside each object name of object , string, seperated " - ", ex. "object1 - 26.05.2012 ". sort array date in string, , not name, descending possible?
as @vladimir pointed out, better separate name , string each other , sort date.
nsmutablearray *newarray = [[nsmutablearray alloc] init]; nsdateformatter *formatter = [[nsdateformatter alloc] init]; [formatter setdateformat:@"dd.mm.yyyy"]; (nsstring *str in yourarray) { nsrange rangefordash = [str rangeofstring:@"-"]; nsstring *objectstr = [str substringtoindex:rangefordash.location]; nsstring *datestr = [str substringfromindex:rangefordash.location+1]; nsdate *date = [formatter datefromstring:datestr]; nsdictionary *dic = [nsdictionary dictionarywithobjectsandkeys:objectstr, @"object", date, @"date", nil]; [newarray addobject:dic]; } nssortdescriptor *sortdesc = [[nssortdescriptor alloc] initwithkey:@"date" ascending:no]; [newarray sortusingdescriptors:[nsarray arraywithobjects:sortdesc, nil]]; [sortdesc release]; [formatter release];
newarray
sorted have dictionary objects, each containing 2 strings.
Comments
Post a Comment