Created
November 20, 2013 11:11
-
-
Save sentient06/7561493 to your computer and use it in GitHub Desktop.
Sorting NSDictionary values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// NSDictionarySorting | |
// | |
// Created by Giancarlo Mariot on 20/11/2013. | |
// Copyright (c) 2013 Giancarlo Mariot. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
NSDictionary * dictionary = [ | |
[NSDictionary alloc] initWithObjectsAndKeys: | |
@"one" , [NSNumber numberWithInt:1] | |
, @"two" , [NSNumber numberWithInt:2] | |
, @"three", [NSNumber numberWithInt:3] | |
, @"four" , [NSNumber numberWithInt:4] | |
, @"five" , [NSNumber numberWithInt:5] | |
, @"six" , [NSNumber numberWithInt:6] | |
, @"seven", [NSNumber numberWithInt:7] | |
, @"eight", [NSNumber numberWithInt:8] | |
, @"nine" , [NSNumber numberWithInt:9] | |
, @"ten" , [NSNumber numberWithInt:10] | |
, nil | |
]; | |
NSLog(@"%@", dictionary); | |
NSArray * sortKeys = [[dictionary allKeys] sortedArrayUsingComparator:^(id obj1, id obj2) { | |
if ([obj1 intValue] > [obj2 intValue]) | |
return (NSComparisonResult)NSOrderedDescending; | |
if ([obj1 intValue] < [obj2 intValue]) | |
return (NSComparisonResult)NSOrderedAscending; | |
return (NSComparisonResult)NSOrderedSame; | |
}]; | |
for (NSNumber * key in sortKeys) { | |
NSLog(@"%@", [dictionary objectForKey:key]); | |
} | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment