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

iOS9 iTune Upload Error : Invalid Bundle. iPad Multitasking support requires launch story board in bundle

1. Add the UIRequiresFullScreen key to your Xcode project’s Info.plist file and apply the Boolean value YES 2. This will fix one more issue that is coming at the time of iTune upload Invalid Bundle. iPad Multitasking support requires these orientations: 'UIInterfaceOrientationPortrait,UIInterfaceOrientationPortraitUpsideDown,UIInterfaceOrientationLandscapeLeft,UIInterfaceOrientationLandscapeRight'. If your application only using single orientation then use the above keyword in info.plist

XCConfig files are not properly deintegrated

After looking into the different block and searched over the internet, still not able to find a valid reason for this error. I also tried "pod deintegrate" but that will also not worked for me. After installing pods, I got a warning message(pasted below). It seems, this is the reason for XCConfig error. This may be because my team member using older version of CocoaPods.. [!] The version of CocoaPods used to generate the lockfile (1.6.1) is higher than the version of the current executable (1.5.2). Incompatibility issues may arise. To fix this issue I follow the below steps, maybe this will work for you. 1. Right click on xxx.xcodeproj file and click on "Show Package Contents" 2. Open "project.pbxproj" file in edit mode 3. Search " Pods; " in opened file 4. Change : path = Pods; to name = Pods; NOTE:  "path" for the newer version and "name" for the older version. Please let me know if it works for you or...