【发布时间】:2012-08-05 14:56:25
【问题描述】:
我想对包含值的 NSMutablearray 进行排序 434,34,4,45
我正在使用此代码进行排序:
[arrayfinale sortUsingSelector:@selector(compare:)];
但它会返回:
45,4,34,433
但我想要
4,34,45,433
谁能帮我解决这个问题
【问题讨论】:
我想对包含值的 NSMutablearray 进行排序 434,34,4,45
我正在使用此代码进行排序:
[arrayfinale sortUsingSelector:@selector(compare:)];
但它会返回:
45,4,34,433
但我想要
4,34,45,433
谁能帮我解决这个问题
【问题讨论】:
使用以下代码
NSArray *unsortedArray = [NSArray arrayWithObjects:@"60", @"4", @"60", @"50", @"12", @"10", @"1", nil];
NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(id firstObject, id secondObject) {
return [((NSString *)firstObject) compare:((NSString *)secondObject) options:NSNumericSearch]; }];
NSLog(@"Array is ::%@",sortedArray);
【讨论】:
NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
【讨论】: