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.
Asked
Active
Viewed 212 times
-1

JustMe
- 306
- 1
- 4
- 15
-
1Show more code please. – Tony Aug 04 '16 at 02:40
-
@TonyHan is there any code in particular you'd like to see? I'm not entirely sure what to add. – JustMe Aug 04 '16 at 02:42
-
`@selector(compare:)`? – Tony Aug 04 '16 at 02:54
-
@TonyHan ``@selector(compare:)`` just compares the strings and puts them in order. There's an explanation [here](http://stackoverflow.com/questions/9242588/sortedarrayusingselector-what-is-it-doing) – JustMe Aug 04 '16 at 03:05
1 Answers
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~~~

CJiYI
- 55
- 1
- 4