ios - sortedArrayUsingComparator returning incorrect results -
ios - sortedArrayUsingComparator returning incorrect results -
i have array has set of timestamps (as strings) , in sorted order. want sort them 1 time again , see if same list returned (reason i'll dealing farther timestamps stored out of order , need sort them). here dates array timestamps strings:
( "jun 18, 2014 12:00:00 am", "jun 18, 2014 12:15:00 am", "jun 18, 2014 12:30:00 am", "jun 18, 2014 12:45:00 am", "jun 18, 2014 1:00:00 am", "jun 18, 2014 1:15:00 am", "jun 18, 2014 1:30:00 am", "jun 18, 2014 1:45:00 am", "jun 18, 2014 2:00:00 am", "jun 18, 2014 2:15:00 am", "jun 18, 2014 2:30:00 am", "jun 18, 2014 2:45:00 am", "jun 18, 2014 3:00:00 am", "jun 18, 2014 3:15:00 am", "jun 18, 2014 3:30:00 am", "jun 18, 2014 4:15:00 am", "jun 18, 2014 4:30:00 am", "jun 18, 2014 4:45:00 am", "jun 18, 2014 5:15:00 am", "jun 18, 2014 5:30:00 am", "jun 18, 2014 5:45:00 am", "jun 18, 2014 6:00:00 am", "jun 18, 2014 6:15:00 am", "jun 18, 2014 6:30:00 am", "jun 18, 2014 6:45:00 am", "jun 18, 2014 7:00:00 am", "jun 18, 2014 7:15:00 am", "jun 18, 2014 7:30:00 am", "jun 18, 2014 7:45:00 am", "jun 18, 2014 8:00:00 am", "jun 18, 2014 8:15:00 am", "jun 18, 2014 8:30:00 am", "jun 18, 2014 8:45:00 am", "jun 18, 2014 9:00:00 am", "jun 18, 2014 9:15:00 am", "jun 18, 2014 10:00:00 am", "jun 18, 2014 10:15:00 am", "jun 18, 2014 10:30:00 am", "jun 18, 2014 10:45:00 am", "jun 18, 2014 11:15:00 am", "jun 18, 2014 11:30:00 am", "jun 18, 2014 11:45:00 am", "jun 18, 2014 12:00:00 pm", "jun 18, 2014 12:15:00 pm", "jun 18, 2014 12:30:00 pm", "jun 18, 2014 12:45:00 pm", "jun 18, 2014 1:00:00 pm", "jun 18, 2014 1:15:00 pm", "jun 18, 2014 1:30:00 pm", "jun 18, 2014 2:00:00 pm", "jun 18, 2014 2:15:00 pm", "jun 18, 2014 2:30:00 pm", "jun 18, 2014 2:45:00 pm", "jun 18, 2014 3:00:00 pm", "jun 18, 2014 3:15:00 pm", "jun 18, 2014 3:45:00 pm", "jun 18, 2014 4:00:00 pm", "jun 18, 2014 4:15:00 pm", "jun 18, 2014 4:45:00 pm", "jun 18, 2014 5:15:00 pm", "jun 18, 2014 5:30:00 pm", "jun 18, 2014 6:00:00 pm", "jun 18, 2014 6:15:00 pm", "jun 18, 2014 7:00:00 pm", "jun 18, 2014 7:15:00 pm", "jun 18, 2014 7:30:00 pm", "jun 18, 2014 8:00:00 pm", "jun 18, 2014 8:15:00 pm", "jun 18, 2014 8:30:00 pm", "jun 18, 2014 9:15:00 pm", "jun 18, 2014 9:30:00 pm", "jun 18, 2014 9:45:00 pm", "jun 18, 2014 10:15:00 pm", "jun 18, 2014 10:30:00 pm", "jun 18, 2014 11:15:00 pm", "jun 18, 2014 11:30:00 pm", "jun 18, 2014 11:45:00 pm" )
the above array in sorted timestamps. now, if re-sort them in ascending order, should give me same results suppose getting out of order results. don't understand why seeing such behavior: here output getting:
sorted dates = ( "jun 18, 2014 12:00:00 am", "jun 18, 2014 1:00:00 am", "jun 18, 2014 2:00:00 am", "jun 18, 2014 3:00:00 am", "jun 18, 2014 6:00:00 am", "jun 18, 2014 7:00:00 am", "jun 18, 2014 8:00:00 am", "jun 18, 2014 9:00:00 am", "jun 18, 2014 10:00:00 am", "jun 18, 2014 12:15:00 am", "jun 18, 2014 1:15:00 am", "jun 18, 2014 2:15:00 am", "jun 18, 2014 3:15:00 am", "jun 18, 2014 4:15:00 am", "jun 18, 2014 5:15:00 am", "jun 18, 2014 6:15:00 am", "jun 18, 2014 7:15:00 am", "jun 18, 2014 8:15:00 am", "jun 18, 2014 9:15:00 am", "jun 18, 2014 10:15:00 am", "jun 18, 2014 11:15:00 am", "jun 18, 2014 12:30:00 am", "jun 18, 2014 1:30:00 am", "jun 18, 2014 2:30:00 am", "jun 18, 2014 3:30:00 am", "jun 18, 2014 4:30:00 am", "jun 18, 2014 5:30:00 am", "jun 18, 2014 6:30:00 am", "jun 18, 2014 7:30:00 am", "jun 18, 2014 8:30:00 am", "jun 18, 2014 10:30:00 am", "jun 18, 2014 11:30:00 am", "jun 18, 2014 12:45:00 am", "jun 18, 2014 1:45:00 am", "jun 18, 2014 2:45:00 am", "jun 18, 2014 4:45:00 am", "jun 18, 2014 5:45:00 am", "jun 18, 2014 6:45:00 am", "jun 18, 2014 7:45:00 am", "jun 18, 2014 8:45:00 am", "jun 18, 2014 10:45:00 am", "jun 18, 2014 11:45:00 am", "jun 18, 2014 12:00:00 pm", "jun 18, 2014 1:00:00 pm", "jun 18, 2014 2:00:00 pm", "jun 18, 2014 3:00:00 pm", "jun 18, 2014 4:00:00 pm", "jun 18, 2014 6:00:00 pm", "jun 18, 2014 7:00:00 pm", "jun 18, 2014 8:00:00 pm", "jun 18, 2014 12:15:00 pm", "jun 18, 2014 1:15:00 pm", "jun 18, 2014 2:15:00 pm", "jun 18, 2014 3:15:00 pm", "jun 18, 2014 4:15:00 pm", "jun 18, 2014 5:15:00 pm", "jun 18, 2014 6:15:00 pm", "jun 18, 2014 7:15:00 pm", "jun 18, 2014 8:15:00 pm", "jun 18, 2014 9:15:00 pm", "jun 18, 2014 10:15:00 pm", "jun 18, 2014 11:15:00 pm", "jun 18, 2014 12:30:00 pm", "jun 18, 2014 1:30:00 pm", "jun 18, 2014 2:30:00 pm", "jun 18, 2014 5:30:00 pm", "jun 18, 2014 7:30:00 pm", "jun 18, 2014 8:30:00 pm", "jun 18, 2014 9:30:00 pm", "jun 18, 2014 10:30:00 pm", "jun 18, 2014 11:30:00 pm", "jun 18, 2014 12:45:00 pm", "jun 18, 2014 2:45:00 pm", "jun 18, 2014 3:45:00 pm", "jun 18, 2014 4:45:00 pm", "jun 18, 2014 9:45:00 pm", "jun 18, 2014 11:45:00 pm" )
for example, why "jun 18, 2014 12:15:00 am", displayed after "jun 18, 2014 1:00:00 am"? don't part!!
here code:
-(void)sortdates { nsdateformatter *dateformatter; dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@"mmm dd, yyyy hh:mm:ss a"]; nslog(@"%@", xaxisdatawithdate); nsarray *sortedtimes = [xaxisdatawithdate sortedarrayusingcomparator:^nscomparisonresult(nsstring *obj1, nsstring *obj2) { nsdate *date1 = [dateformatter datefromstring:obj1]; nsdate *date2 = [dateformatter datefromstring:obj2]; homecoming [date1 compare:date2]; }]; nslog(@"sorted dates = %@", sortedtimes); }
i wrote sample programme subset containing these dates , seems give me wrong result when 1:00:00 nowadays in array (1:00:00 should have been stored after 12:00:00 am):
-(void)sortdates { nsdateformatter *dateformatter; dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@"mmm dd, yyyy hh:mm:ss a"]; nsmutablearray *times = [[nsmutablearray alloc]initwithobjects:@"jun 19, 2014 11:45:00 pm", @"jun 18, 2014 1:00:00 am", @"jun 18, 2014 12:15:00 am",@"jun 18, 2014 12:00:00 am", nil]; nsarray *sortedtimes = [times sortedarrayusingcomparator:^nscomparisonresult(nsstring *obj1, nsstring *obj2) { nsdate *date1 = [dateformatter datefromstring:obj1]; nsdate *date2 = [dateformatter datefromstring:obj2]; homecoming [date1 compare:date2]; }]; nslog(@"sorted dates = %@", sortedtimes); }
output:
sorted dates = ( "jun 18, 2014 1:00:00 am", "jun 18, 2014 12:00:00 am", "jun 18, 2014 12:15:00 am", "jun 19, 2014 11:45:00 pm" )
i can't figure out why getting out of order results w.r.t nsmutablearray *xaxisdatawithdate , w.r.t nsmutablearray *times! kindly help me in regard! appreciated.
you seem have wrong format, time appears in 12 hr format, alter format mmm dd, yyyy hh:mm:ss a
note lowercase "h"
"jun 18, 2014 12:00:00 am", "jun 18, 2014 12:15:00 am", "jun 18, 2014 1:00:00 am", "jun 19, 2014 11:45:00 pm"
more importantly, should convert strings dates prior comparison, sortedarray…
using merged sort, hence convert string dates n log n
times instead of n
times
ios iphone objective-c nsdate nsdateformatter
Comments
Post a Comment