-1

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.

JustMe
  • 306
  • 1
  • 4
  • 15

1 Answers1

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