Question

Long story short, I have begun working with uitableview a not long ago. I finally got everything down except for proper sorting.

I can sort ascending and descending but not numerically

I have been told that I need to use nsdictionary or nsnumber and compare results to sort proper

In other words I have the nsmutablearray numberArray... And it adds numbers but in string format so the numbers 25, 32, 15 and 3 will show in the view as, 32, 3, 25 and 15.

I have looked and read for hours, hours of testing with different codes and such nix any get any results... Can anyone please direct me?

Was it helpful?

Solution

You need to sort on 'intValue' of the strings:

NSMutableArray *toBeSorted = [NSMutableArray array];
...add stuff to the array...
NSSortDescriptor *mySorter = [[[NSSortDescriptor alloc] initWithKey:@"intValue" ascending:YES] autorelease];
[toBeSorted sortUsingDescriptors:[NSArray arrayWithObject:mySorter]];

So while the above will work, tc's suggestion of using:

toBeSorted = [toBeSorted sortedArrayUsingSelector:@selector(localizedStandardCompare:)];

is a more elegant solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top