MarcoA
MarcoA

Reputation: 23

IOS sort NSMutableArray for a numeric field

I hope that there is somebody outside, who can help me with this:

I have an unsorted array of dictionaries which have a STRING field (Km) inside that contains numbers (e.g. 12,0; 1,2; 6,4). If I do a normal SORT, the array will be sorted that way: 1,2 12,0 6,4

But it should sorted that way: 1,2 6,4 12,0

Does anybody has an example code how to do this? I'm looking forward to get an answer.

Thanks and regards Marco

Edit my code for further understanding:

NSMutableArray *tempArray = [[NSArray alloc] init];
self.Digger = tempArray;
[tempArray release];

self.Digger = [self.Diggers objectForKey:@"Rows"];

NSSortDescriptor * sort = [[[NSSortDescriptor alloc] initWithKey:@"KM" ascending:true] autorelease]; [self.Digger sortUsingDescriptors:[NSArray arrayWithObject:sort]];
self.Digger = sort;

But this gives me the follwoing error in the log: -[NSSortDescriptor count]: unrecognized selector sent to instance

Upvotes: 2

Views: 8290

Answers (2)

zaph
zaph

Reputation: 112855

It is best to put numbers into the dictionary as NSNumbers rather than strings. This avoids two problems:


1 The localization if decimal numbers, 1.2 vs 1,2 which will work differently depending un the user's location in the world.
2. The natural sort order of numbers is what is desired.

NSArray *a = [NSArray arrayWithObjects:
              [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:12.0] forKey:@"Km"],
              [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat: 1.2] forKey:@"Km"],
              [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat: 6.4] forKey:@"Km"],
              nil];

NSSortDescriptor * sort = [[[NSSortDescriptor alloc] initWithKey:@"Km" ascending:true] autorelease];
NSArray *sa = [a sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
NSLog(@"sa: %@", sa);

NSLog output:

2011-10-30 10:08:58.791 TestNoARC[86602:f803] sa: (
                                                   {
                                                       Km = "1.2";
                                                   },
                                                   {
                                                       Km = "6.4";
                                                   },
                                                   {
                                                       Km = 12;
                                                   }
                                                   )

Upvotes: 3

Jesse Black
Jesse Black

Reputation: 7986

// assume toBeSorted is your array of dictionaries and the key for the int values is @"myInt"
NSSortDescriptor *sort = [[[NSSortDescriptor alloc] initWithKey:@"myInt" ascending:YES] autorelease];
[toBeSorted sortUsingDescriptors:[NSArray arrayWithObject:sort]];

This gets you close to where you want. If you are always using nonnegative numbers, you can then check if the first object represents your 0 value and move it to the back of the array.

Upvotes: 0

Related Questions