4

我正在尝试对一系列价格从低到高进行排序。我让它工作,但不是我想要的方式。长话短说,分拣机将数字按如下顺序排列: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];
}

有人可以告诉我我需要在这里做什么吗?提前致谢。

4

2 回答 2

10

像这样创建排序描述符:

sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"ListPrice"
                                               ascending:YES
                                              comparator:^(id obj1, id obj2) {
    return [obj1 compare:obj2 options:NSNumericSearch];
}];
于 2010-12-28T23:50:34.117 回答
0

您需要先将 NSString 转换为 NSNumbers。查看文档NSNumberFormatter和实例方法numberFromString

于 2010-12-28T23:50:15.963 回答