如何将数组中的数字从低到高排序
我正在尝试将一系列价格从低到高排序。我可以让它工作,但不是我想要的方式。长话短说,排序器将数字按如下顺序排列: 100 10900 200 290
而不是像这样排序
100 200 290 10900
这是我正在执行此操作的代码。
-(void)filterPriceLowHigh {
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"ListPrice"
ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [app.vehiclesArrayToDisplay
sortedArrayUsingDescriptors:sortDescriptors];
[app.vehiclesArrayToDisplay removeAllObjects];
[app.vehiclesArrayToDisplay addObjectsFromArray:sortedArray];
}
有人可以告诉我我需要在这里做什么吗? 提前致谢。
I am trying to sort an array of prices from low to high. I have it working but not the way I want it to. Long story short, the sorter is putting numbers in order like this:
100
10900
200
290
instead of sorting like this
100
200
290
10900
here is my code I am doing this with.
-(void)filterPriceLowHigh {
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"ListPrice"
ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [app.vehiclesArrayToDisplay
sortedArrayUsingDescriptors:sortDescriptors];
[app.vehiclesArrayToDisplay removeAllObjects];
[app.vehiclesArrayToDisplay addObjectsFromArray:sortedArray];
}
Could someone tell me what I need to do here?
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样创建排序描述符:
Create the sort descriptor like this:
您需要首先将 NSString 转换为 NSNumbers。查看
NSNumberFormatter
和实例方法numberFromString
的文档。You need to convert your NSString's to NSNumbers first. Look at the documentation for
NSNumberFormatter
and the instance methodnumberFromString
.