Skip to main content

How to sort NSMutableArray using sortedArrayUsingDescriptors?

http://stackoverflow.com/questions/1844031/how-to-sort-nsmutablearray-using-sortedarrayusingdescriptors

Example using NSString and NSNumber values with sorting on NSNumber value:



NSString * NAME      = @"name";
NSString * ADDRESS   = @"address";
NSString * FREQUENCY = @"frequency";
NSString * TYPE      = @"type";
NSMutableArray * array = [NSMutableArray array];
NSDictionary * dict;

dict = [NSDictionary dictionaryWithObjectsAndKeys:
            @"Alehandro", NAME, @"Sydney", ADDRESS,
            [NSNumber numberWithInt:100], FREQUENCY,
            @"T", TYPE, nil];
[array addObject:dict];

dict = [NSDictionary dictionaryWithObjectsAndKeys:
            @"Xentro", NAME, @"Melbourne", ADDRESS,
            [NSNumber numberWithInt:50], FREQUENCY,
            @"X", TYPE, nil];
[array addObject:dict];

dict = [NSDictionary dictionaryWithObjectsAndKeys:
            @"John", NAME, @"Perth", ADDRESS,
            [NSNumber numberWithInt:75],
            FREQUENCY, @"A", TYPE, nil];
[array addObject:dict];

dict = [NSDictionary dictionaryWithObjectsAndKeys:
            @"Fjord", NAME, @"Brisbane", ADDRESS,
            [NSNumber numberWithInt:20], FREQUENCY,
            @"B", TYPE, nil];
[array addObject:dict];



Sorting part using descriptors with the Frequency field which is NSNumber:


NSSortDescriptor * frequencyDescriptor =
    [[[NSSortDescriptor alloc] initWithKey:FREQUENCY
                                 ascending:YES] autorelease];

id obj;
NSEnumerator * enumerator = [array objectEnumerator];
while ((obj = [enumerator nextObject])) NSLog(@"%@", obj);
NSArray * descriptors =
    [NSArray arrayWithObjects:frequencyDescriptor, nil];
NSArray * sortedArray =
    [array sortedArrayUsingDescriptors:descriptors];
NSLog(@"\nSorted ...");

enumerator = [sortedArray objectEnumerator];
while ((obj = [enumerator nextObject])) NSLog(@"%@", obj);


OUTPUT - sorted by Frequency field:

2009-12-04 x[1] {address = Sydney; frequency = 100; name = Alehandro; type = T; }
2009-12-04 x[1] {address = Melbourne; frequency = 50; name = Xentro; type = X; }
2009-12-04 x[1] {address = Perth; frequency = 75; name = John; type = A; }
2009-12-04 x[1] {address = Brisbane; frequency = 20; name = Fjord; type = B; }
2009-12-04 x[1]
Sorted ...
2009-12-04 x[1] {address = Brisbane; frequency = 20; name = Fjord; type = B; }
2009-12-04 x[1] {address = Melbourne; frequency = 50; name = Xentro; type = X; }
2009-12-04 x[1] {address = Perth; frequency = 75; name = John; type = A; }
2009-12-04 x[1] {address = Sydney; frequency = 100; name = Alehandro; type = T; }

Comments

Popular posts from this blog

Fixed : App crash on update with core data changes

Why migration required ? When the model does not match the store, a migration is required. In order to perform a migration, Core Data (technically, an instance of NSMigrationManager) requires these things: The destination managed object model (the one with the changes) A managed object model that can open the existing store The ability to infer mapping between the two models (for a lightweight migration), or a manual mapping model Permission to attempt to perform an automatic migration (for a lightweight migration) It is therefore absolutely essential that you never make changes to the managed object model for a released version of an app. That is, if your app is already in the App Store,  don’t change a single thing in that version of the managed object model . Solution for the Crash while update an application from the app store: Create a  new version  of the managed object model! This reminds me to mention some other best practices to adopt when worki...

Code Review Tool for Objective-C

Clang Static Analyzer The Clang Static Analyzer is a source code analysis tool that finds bugs in C, C++, and Objective-C programs. Currently it can be run either as a  standalone tool  or  within Xcode . The standalone tool is invoked from the command line, and is intended to be run in tandem with a build of a codebase. The analyzer is 100% open source and is part of the  Clang project. Like the rest of Clang, the analyzer is implemented as a C++ library that can be used by other tools and applications. Download Mac OS X Latest build (Intel-only binary, 10.5+): checker-275.tar.bz2  (built May 23, 2013) Release notes This build can be used both from the command line and from within Xcode Installation  and  usage Other Platforms For other platforms, please follow the instructions for  building the analyzer  from source code. Viewing static analyzer results in Xcode Viewing static analyzer results in a web ...