IOS Sorts NSString's NSArray Time

I need to sort NSArray NSString time e.g.

 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]; 

I need to sort the array in ascending order.
Is there any way to do this?

+6
source share
4 answers
 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"]; NSArray *sortedTimes = [times sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) { NSDate *date1 = [dateFormatter dateFromString:obj1]; NSDate *date2 = [dateFormatter dateFromString:obj2]; return [date1 compare:date2]; }]; 

optimized version:

 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]; } 
+10
source

You can try this code:

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

Since these are strings, they will be saved as

01:15, 12:25, 05:00 ....

And they are not NSDate .

So what you need to do is to create a parallel array with NSDate from these lines , sort the array and extract these values.


During the introduction, I solved it with the help of a beginner

 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]; NSMutableArray *dates=[NSMutableArray new]; NSDateFormatter *dateFormatter=[NSDateFormatter new]; [dateFormatter setDateFormat:@"hh:mm a"]; for (NSString *stringDate in times) { NSDate *date=[dateFormatter dateFromString:stringDate]; [dates addObject:date]; } NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES]; NSArray *descriptors = [NSArray arrayWithObject: descriptor]; NSArray *reverseOrder = [dates sortedArrayUsingDescriptors:descriptors]; [times removeAllObjects]; for (NSDate *date in reverseOrder) { NSString *string=[dateFormatter stringFromDate:date]; [times addObject:string]; } NSLog(@"%@",times); 
+3
source

To compare NSDate use this:

 + (BOOL)isDate:(NSDate *)date1 smallerThanAnotherDate:(NSDate *)date2 { NSDate* enddate = date1; NSDate* currentdate = date2; NSTimeInterval distanceBetweenDates = [enddate timeIntervalSinceDate:currentdate]; double secondsInMinute = 60; NSInteger secondsBetweenDates = distanceBetweenDates / secondsInMinute; if (secondsBetweenDates == 0) return YES; else if (secondsBetweenDates < 0) return YES; else return NO; } 

So, if you do not want to convert your watch to NSDate, do it yourself, this will work for me.

 NSArray *sortedTimes = [times sortedArrayUsingSelector:@selector(localizedStandardCompare:)]; 
0
source

Source: https://habr.com/ru/post/943596/


All Articles