I have several NSStrings that contains time duration, they look like that: @"03:40", @"08:40" – the time is duration of an action.
I want to have an int for each string with minutes, say “01:20” is “80” (minutes) to later compare them with one another.
How do you manage this simple thing in Objective-C?
Thanks in advance
EDIT:
Tried this, no avail. I want a totally different approach, something easy and light.
- (NSMutableArray *)sortResultsByTime
{
NSMutableArray *sortedTrips = [NSMutableArray arrayWithArray:[_tweets sortedArrayUsingComparator: ^(id trip1, id trip2) {
double time1 = [self durationFromString:[(Tweet *) trip1 flightDuration]];
double time2 = [self durationFromString:[(Tweet *) trip1 flightDuration]];
NSLog(@"What's time1? It's %f", time1);
NSLog(@"What's time2? It's %f", time2);
if (time1 < time2) {
return (NSComparisonResult)NSOrderedDescending;
}
if (time1 > time2) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}]];
return sortedTrips;
}
- (double)durationFromString:(NSString *)durationString
{
NSArray *durationArray = [durationString componentsSeparatedByString:@":"];
return [durationArray[0] doubleValue] + [durationArray[1] doubleValue] / 60.0;
}
The code you posted is attempting to convert the string to the number of hours, not the number of minutes.
You want:
Edit: The original code is failing because this line:
needs to be:
Update: Since the OP wants a different approach why not simply do this: