Monday, 12 August 2013

Sorting array based on date and time with two dictionary keys

Sorting array based on date and time with two dictionary keys

I am trying to sort an array based on date and time , I can successfully
sort the array based on date both time is coming as another value in
dictionary.
So date comes as a string in format "yyyy-MM-dd" and time comes in as a
string in format "HH:mm"
Time value comes in key "starts" as string '"HH:mm"' format.
I know somehow I need to combine two strings to 'yyyy-MM-dd HH:mm' but how?
-(NSMutableArray *)sortArrayBasedOndate:(NSMutableArray *)arraytoSort
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSComparator compareDates = ^(id string1, id string2)
{
NSDate *date1 = [formatter dateFromString:string1];
NSDate *date2 = [formatter dateFromString:string2];
return [date1 compare:date2];
};
NSSortDescriptor * sortDesc1 = [[NSSortDescriptor alloc]
initWithKey:@"start_date" ascending:YES comparator:compareDates];
[arraytoSort sortUsingDescriptors:[NSArray arrayWithObjects:sortDesc1,
nil]];
return arraytoSort;
}
Any idea how can I solve this problem?

No comments:

Post a Comment