Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

alexanderl

macrumors newbie
Original poster
Jan 16, 2009
8
0
Hi All!
(I have to add that Im new to ObjC dev)

I have a NSMutableArray that contains NSDictionary objects, which each contain an object and a float (NSNumber).

Now I want to sort the Array based on the float... How do I do that?

I'm trying with sortUsingSelector: but I dont get it to work....

Thanks for your help!
alex
 
Thanks caveman_uk!

Ok here is what I got so far (and what doesnt work)

[resultsForThisTestData addObject:[NSDictionary dictionaryWithObjectsAndKeys:dpp, @"DataPoint",
[NSNumber numberWithFloat:distance],@"Distance",nil]];


NSSortDescriptor * sortByDistance = [[[NSSortDescriptor alloc] initWithKey:mad:"Distance" ascending:TRUE] autorelease];

[resultdata initWithArray:[results sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortByDistance, nil]]];

(hope that is all the code you need)

As I said, this doesnt work and I dont see why?!?!

Thanks!
Alex
 
Check out the official documentation: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Collections/Articles/sortingFilteringArrays.html

This example helped me out:
Code:
NSSortDescriptor *lastDescriptor =

        [[[NSSortDescriptor alloc] initWithKey:LAST

                                   ascending:NO

                                   selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];

NSSortDescriptor *firstDescriptor =

    [[[NSSortDescriptor alloc] initWithKey:FIRST

                               ascending:NO

                               selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];

NSArray *descriptors = [NSArray arrayWithObjects:firstDescriptor, lastDescriptor, nil];

sortedArray = [array sortedArrayUsingDescriptors:descriptors];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.