I have an iOS app with an NSMutableArray
that gets displayed in a UITableView
. The array usually contains only numbers (as that is the main purpose), but occasionally contains some letters. The final thing that happens to the array before being displayed is sorting by alphabetical order using [customerArray sortUsingSelector:@selector(compare:)];
. However, when I run the app, if the array contains numbers with different amounts of digits (ex: 0 - 20), the 10s get sorted right after the 1 rather than after 9. Is there a way to get the numbers properly sorted by numerical value? I'll gladly give more information, code or screenshots if necessary. Any help is much appreciated.
我有一个带有NSMutableArray的iOS应用程序,可以在UITableView中显示。数组通常只包含数字(这是主要目的),但偶尔也包含一些字母。在显示之前,数组的最后一件事是使用[customerArray sortUsingSelector:@selector(compare::)]按字母顺序排序;但是,当我运行这个应用程序时,如果数组中包含的数字是不同的数字(ex: 0 - 20),那么10个数字就会在1后而不是9后进行排序。有没有一种方法可以让数字正确地按数值排序?如有必要,我将乐意提供更多的信息、代码或截图。非常感谢您的帮助。
1 个解决方案
#1
0
This thinking of my code may help you:
我的这段代码可能会帮到你:
NSArray *arr = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20"];
NSMutableArray *arrM = [NSMutableArray arrayWithArray:arr];
NSArray *arr2= [arrM sortedArrayUsingComparator:^(id objA,id objB){
return (NSInteger)([objA integerValue] > [objB integerValue]);//ascending
//return (NSInteger)([objA integerValue] < [objB integerValue]);//descending
}];
NSLog(@"%@",arr2);
Hope it help you ! hahahahahha~~~
希望它能帮助你!hahahahahha ~ ~ ~
#1
0
This thinking of my code may help you:
我的这段代码可能会帮到你:
NSArray *arr = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20"];
NSMutableArray *arrM = [NSMutableArray arrayWithArray:arr];
NSArray *arr2= [arrM sortedArrayUsingComparator:^(id objA,id objB){
return (NSInteger)([objA integerValue] > [objB integerValue]);//ascending
//return (NSInteger)([objA integerValue] < [objB integerValue]);//descending
}];
NSLog(@"%@",arr2);
Hope it help you ! hahahahahha~~~
希望它能帮助你!hahahahahha ~ ~ ~