2

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

2 Answers 2

3

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;
                                                   }
                                                   )
Sign up to request clarification or add additional context in comments.

7 Comments

The sort works that way and this is the reason, why I will set this answer as my accepted answer. Many Thanks to CocoaFu!
But now I have a hugh follow up problem!
that happens, the best is probably to formulate a good question and create another question.
But now I have a hugh follow up problem! I have to set parameter type to REAL in my XML-File that this is working and now I get problems in the methode cellForRowAtIndexPath to set my values to the cell. Is there a way to convert the sorted array (with the real Value in it) back to array with only string values. The problem is, I get no error, but only the app stops before showing the tableview. I played around a little bit an somethimes I get the error -[UIImage length]: unrecognized selector sent to instance. Could anybody give me further help on that? Thanks!
Get the NSNumber value from the dictionary, convert it to a float with: - (float)floatValue and format as desired with + (id)stringWithFormat:(NSString *)format, .... Or convert the NSNumber value directly to a NSString with - (NSString *)stringValue but you will get the default formatting and that may not be what you want.
|
0
// 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.

6 Comments

My guess is that the comma in the numbers is a decimal point as per some localizations so there are really only 3 values in the question.
Cool. If that is the case, this answer should sort those numbers "(e.g. 12,0; 1,2; 6,4)." as desired
As desired is: 1,2 6,4 12,0. Here is my guess, these are distances in kilometers (Km) and the sort is desired by distance. in U.S. localization: 1.2km, 6.4km, 12.0km.
Ok, I try to give you an easier example. Assume that I have the following numbers inside: 1, 2, 10 and 102. I will get the following sort: 1, 10, 102, 20! This is what I called a STRING sort. What I need is a numeric sort of the numbers, that I get 1, 2, 20, 102.
Can you try it by populating your array with another key that is a NSNumber object and sorting by that key?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.