ios - sorting array with block does not return the right result -
this question has answer here:
- stringfromdate returning wrong year 1 answer
so have array of strings (monthsarray) looks this:
months array before :( "november 2012", "january 2013", "september 2013", "december 2012", "august 2013" )
and use sort :
nsdateformatter * formatter=[[nsdateformatter alloc]init]; [formatter setdateformat:@"mmmm yyyy"]; monthsarray = [nsmutablearray arraywitharray:[tempmonthsarray sortedarrayusingcomparator:^nscomparisonresult(id a, id b) { nstimeinterval firsttime=[[formatter datefromstring:(nsstring*)a]timeintervalsince1970]; nstimeinterval secondtime=[[formatter datefromstring:(nsstring*)b]timeintervalsince1970]; return [[nsnumber numberwithdouble:firsttime]compare:[nsnumber numberwithdouble:secondtime]]; }]];
after sort it looks :
months array after :( "november 2012", "december 2012", "january 2013", "september 2013", "august 2013" )
as see in before , after 'sort' array..they're not arranged properly. why not? cant see wrong code.
looks incorrect format. try this.
[formatter setdateformat:@"mmmm yyyy"];
@"yyyy" week-based calendar year.
@"yyyy" ordinary calendar year.
refer here more explanation
Comments
Post a Comment