7 Apr 2017

Sort an NSArray of Time NSString's(@"09:00 AM",@"07:30 AM",@"06:45 PM",@"05:00 PM",@"12:45 AM",@"12:45 PM",@"01:00 AM",@"01:15 PM", nil];)...


Sort an NSArray of Time NSString's(@"09:00 AM",@"07:30 AM",@"06:45 PM",@"05:00 PM",@"12:45 AM",@"12:45 PM",@"01:00 AM",@"01:15 PM", nil];)...



NSString *timeSlotStr = @"03:00,04:00,01:30,02:30,04:31,06:00,07:00,04:30,04:29,05:00";
    NSArray* Array = [timeSlotStr  componentsSeparatedByString:@","];
    
    NSMutableArray *Array2 = [[NSMutableArray alloc]initWithArray:Array];
      [Array2 sortUsingSelector:@selector(compare:)];
    
     NSLog(@"Array======> %@",Array2);

  /*******************************************************************/


NSMutableArray *times = [[NSMutableArray alloc]initWithObjects:@"09:00 AM",@"07:30 AM",@"06:45 PM",@"05:00 PM",@"12:45 AM",@"12:45 PM",@"01:00 AM",@"01:15 PM", nil];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:mm a"];
    
    NSMutableArray *dates = [NSMutableArray arrayWithCapacity:times.count];
    for (NSString *timeString in times)
    {
        NSDate *date = [dateFormatter dateFromString:timeString];
        [dates addObject:date];
    }
    
    [dates sortUsingSelector:@selector(compare:)];
    
    NSMutableArray *sortedTimes = [NSMutableArray arrayWithCapacity:dates.count];
    for (NSDate *date in dates)
    {
        NSString *timeString = [dateFormatter stringFromDate:date];
        [sortedTimes addObject:timeString];
    }
      NSLog(@"sortedTimes======> %@",sortedTimes);




Result:

2017-04-07 14:45:31.219 AboutDates[1182:81276] Array======> (
    "01:30",
    "02:30",
    "03:00",
    "04:00",
    "04:29",
    "04:30",
    "04:31",
    "05:00",
    "06:00",
    "07:00"
)
2017-04-07 14:45:31.221 AboutDates[1182:81276] sortedTimes======> (
    "12:45 AM",
    "01:00 AM",
    "07:30 AM",
    "09:00 AM",
    "12:45 PM",
    "01:15 PM",
    "05:00 PM",
    "06:45 PM"
)

No comments:

Post a Comment

Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0