ios - sorting NSArray of NSDictionaries in which are array of dictionaries -
i need sorting array "date" key of items dictionary.and in end need array structure mdata array sorting date.
mdata---------->( { items = ( { accuracy = 5; address = "australia, sydney cbd, bridge street"; course = "0.00"; date = "2013-08-20 06:50:28 +0000"; speed = 3; alt = 0; lat = "-33.8634"; lon = "151.211"; }, { accuracy = 65; address = "armenia, yerevan, \u053f\u0561\u056c\u0565\u0576\u0581\u056b \u0583\u0578\u0572\u0578\u0581"; course = "0.00"; date = "2013-08-20 06:41:02 +0000"; speed = 3; alt = 1116; lat = "40.20016076"; lon = "44.49
enter code here130268"; } ); name = name; starttime = "10:40"; } )
my array looked like 2.nsdictionary * mfirst = [nsdictionary dictionarywithobjectsandkeys:[nsdate distantfuture], @"date", nil]; nsdictionary * msecond = [nsdictionary dictionarywithobjectsandkeys:[nsdate distantpast], @"date", nil]; nsarray *marr = [[nsarray alloc]initwithobjects:mfirst,msecond, nil]; nsdictionary * mydict = [nsdictionary dictionarywithobject:marr forkey:@"items"]; nsarray * msortedarray = [[ nsarray alloc]initwithobjects:mydict, nil]; nslog(@"%@", msortedarray);
use this:
nssortdescriptor *datedescriptor = [[[nssortdescriptor alloc] initwithkey:@"date" ascending:yes] autorelease]; nsarray *sortdescriptors = [nsarray arraywithobject:datedescriptor]; nsarray *sortedarray = [myarray sortedarrayusingdescriptors:sortdescriptors];
updated: have used this:
nsarray *myarray = [[nsarray alloc] initwithobjects:[nsdictionary dictionarywithobjectsandkeys:[nsdate distantfuture], @"date", nil], [nsdictionary dictionarywithobjectsandkeys:[nsdate distantpast], @"date", nil], nil]; nslog(@"%@", myarray); nssortdescriptor *datedescriptor = [[nssortdescriptor alloc] initwithkey:@"date" ascending:yes]; nsarray *sortdescriptors = [nsarray arraywithobject:datedescriptor]; nsarray *sortedarray = [myarray sortedarrayusingdescriptors:sortdescriptors]; nslog(@"%@", sortedarray);
and got log:
before sorting: ( { date = "4001-01-01 00:00:00 +0000"; }, { date = "0001-12-30 00:00:00 +0000"; } ) after sorting: ( { date = "0001-12-30 00:00:00 +0000"; }, { date = "4001-01-01 00:00:00 +0000"; } )
please check while using above code.
Comments
Post a Comment