NSMutableArray *array = [NSMutableArray arrayWithObjects:@"12",@"84", @"35", @"70", @"85", @"99", nil]; NSInteger count = [array count]; for (int i = 0; i < count; i++) { for (int j = 0; j < count - i - 1; j++) { // if ([[array objectAtIndex:j] intValue] > [[array objectAtIndex:(j + 1)] intValue]) { //这里在用[array objectAtIndex:j]时候必须intValue // if([[array objectAtIndex:j] compare:[array objectAtIndex:j + 1]] == -1){ //这里整体必须有一个返回值,-1,0,1,因为compare的返回值NSComparisonResult是一个枚举类型的值,所以要返回一个值 if([[array objectAtIndex:j] compare:[array objectAtIndex:j + 1] options:NSNumericSearch] == 1){ //同上potions NSNumericSearch = 64, [array exchangeObjectAtIndex:j withObjectAtIndex:(j + 1)]; //这里可以用exchangeObjectAtIndex:方法来交换两个位置的数组元素。 } } } for (NSString *i in array) { NSLog(@"%@", i); } NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"12",@"84", @"35", @"70", @"85", @"99", nil]; [array1 sortUsingSelector:@selector(compare:)]; NSLog(@"%@", array);