Uitableview按数字排序

时间:2013-02-21 01:15:28

标签: iphone ios sorting numerical

长话短说,我不久前就开始与uitableview合作了。除了正确的排序外,我终于把一切都搞定了。

我可以按升序排序,但不能按数字排序

我被告知我需要使用nsdictionary或nsnumber并比较结果以正确排序

换句话说,我有nsmutablearray numberArray ......并且它以字符串格式添加数字,因此数字25,32,15和3将在视图中显示为32,3,25和15。

我已经查看并阅读了几个小时,使用不同代码测试了几个小时,这样的nix得到了任何结果......任何人都可以指导我吗?

1 个答案:

答案 0 :(得分:0)

您需要对字符串的'intValue'进行排序:

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

因此,虽然上述方法有效,但tc建议使用:

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

是一个更优雅的解决方案。