0

私は3本の弦を持っています。

NSString *hour1 = @"10:00";
NSString *hour2 = @"6:00";
NSString *hour3 = @"9:00";

どの文字列の値が大きいかを確認するにはどうすればよいですか? -> 10:00 > 9:00 > 6:00

前もって感謝します!

4

3 に答える 3

3

文字列を配列に追加し、

NSString *hour1 = @"10:00";
NSString *hour2 = @"6:00";
NSString *hour3 = @"9:00";

NSArray *hours = [NSArray arrayWithObjects:hour1, hour2, hour3, nil];

次に、配列をソートし、

NSArray *result = [hours sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
    return ([obj1 compare:obj2 options:NSNumericSearch] == NSOrderedAscending);
}];
于 2012-05-17T06:30:52.673 に答える
1
NSString *hour1 = @"10:00";
NSString *hour2 = @"6:00";
NSString *hour3 = @"9:00";

// Convert the hours over to int's such as 1000, 600, and 900.
int hour1AsInt = [[hour1 stringByReplacingOccurrencesOfString:@":" withString:@""] intValue];
int hour2AsInt = [[hour2 stringByReplacingOccurrencesOfString:@":" withString:@""] intValue];
int hour3AsInt = [[hour3 stringByReplacingOccurrencesOfString:@":" withString:@""] intValue];

// Make the comparison's...  This could be more efficient but works well and is easy to follow:
if (hour1AsInt > hour2AsInt) {
    if (hour1AsInt > hour3AsInt) {
        NSLog(@"Hour 1 is biggest");
        return;
    }
}

if (hour2AsInt > hour1AsInt) {
    if (hour2AsInt > hour3AsInt) {
        NSLog(@"Hour 2 is biggest");
        return;
    }
}

if (hour3AsInt > hour1AsInt) {
    if (hour3AsInt > hour2AsInt) {
        NSLog(@"Hour 3 is biggest");
        return;
    }
}
于 2012-05-17T06:30:09.513 に答える
0

localizedStandardCompareを使用できるはずです。

NSLog(@"%ld",[hour1 localizedStandardCompare:hour2]);
于 2012-05-17T06:30:56.947 に答える