-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.

4
  • 1
    Show more code please. Commented Aug 4, 2016 at 2:40
  • @TonyHan is there any code in particular you'd like to see? I'm not entirely sure what to add. Commented Aug 4, 2016 at 2:42
  • @selector(compare:)? Commented Aug 4, 2016 at 2:54
  • @TonyHan @selector(compare:) just compares the strings and puts them in order. There's an explanation here Commented Aug 4, 2016 at 3:05

1 Answer 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~~~

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.