How to sort numbers in an array from low to high

I am trying to sort an array of prices from low to high. I work, but not the way I want. In short, the sorter puts the numbers as follows: 100 10,900 200,290

instead of sorting the type

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]; } 

Can someone tell me what I need to do here? thanks in advance.

+4
iphone sdk
Dec 28 '10 at 23:32
source share
2 answers

Create a sort descriptor as follows:

 sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"ListPrice" ascending:YES comparator:^(id obj1, id obj2) { return [obj1 compare:obj2 options:NSNumericSearch]; }]; 
+10
Dec 28 '10 at 23:50
source share

First you need to convert NSString to NSNumbers. See the documentation for NSNumberFormatter and the instance method numberFromString .

0
Dec 28 '10 at 23:50
source share



All Articles