Rango de time de sorting en Ios

Tengo una matriz como

<__NSArrayI 0xa8434a0>( { From = "10:01AM"; Title = Discuss; To = "12:00AM"; }, { From = "12:01AM"; Title = Discuss; To = "02:00AM"; }, { From = "5:01AM"; Title = Available; To = "7:00AM"; }, { From = "2:01AM"; Title = Discuss; To = "5:00AM"; }, { From = "7:01AM"; Title = Available; To = "12:00pM"; } ) 

¿Cómo puedo orderar este set según el intervalo de time usando la tecla Desde y hacia?

Intenté como sigue:

Pero no bien

 sortedDatesArray = [[unsortedDatesArray sortedArrayUsingComparator: ^(id a, id b) { NSDate *d1 = [NSDate dateWithString: s1]; NSDate *d2 = [NSDate dateWithString: s2]; return [d1 compare: d2]; }]; 

Ayúdame a resolver esto

 NSArray *toSortArray = nil; /*The array you want to sort*/ NSArray *sortedArray = nil; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"hh:mma"]; NSSortDescriptor *descriptorTo = [NSSortDescriptor sortDescriptorWithKey:@"To" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) { NSDate *firstDate = [formatter dateFromString:obj1]; NSDate *secondDate = [formatter dateFromString:obj2]; return [firstDate compare:secondDate]; }]; NSSortDescriptor *descriptorFrom = [NSSortDescriptor sortDescriptorWithKey:@"From" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) { NSDate *firstDate = [formatter dateFromString:obj1]; NSDate *secondDate = [formatter dateFromString:obj2]; return [firstDate compare:secondDate]; }]; sortedArray = [toSortArray sortedArrayUsingDescriptors:@[descriptorFrom, descriptorTo]]; 

Espero que esta responda a su pregunta. 🙂

Prueba esto :

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"hh:mma"]; [times sortUsingComparator:^NSComparisonResult(NSString* obj1, NSString *obj2) { NSDate *firstDate = [formatter dateFromString:obj1]; NSDate *secondDate = [formatter dateFromString:obj2]; return [firstDate compare:secondDate]; }]; 

Intenté con su código, pruebe el siguiente código. Cambié los campos de tu matriz. O puede cambiar el formatting de date.

 NSMutableArray *timesArr = [[NSMutableArray alloc] initWithObjects:@"05:00 AM",@"08:00 AM",@"10:00 AM",@"05:01 AM" ,@"7:59 AM",@"12:00 AM", nil]; NSDateFormatter *formt = [[NSDateFormatter alloc] init]; [formt setDateFormat:@"hh:mm a"]; [timesArr sortUsingComparator:^NSComparisonResult(NSString* ob1, NSString *ob2) { NSDate *date1 = [formt dateFromString:ob1]; NSDate *date2 = [formt dateFromString:ob2]; return [date1 compare:date2]; }]; NSLog(@"timesArr: %@",timesArr);