Last active
August 29, 2015 14:01
-
-
Save ishaq/45ed5e0201be380b359c to your computer and use it in GitHub Desktop.
Get a list of all country codes sorted by their localised names
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
+ (NSArray *)getSortedCountryCodes { | |
NSLocale *locale = [NSLocale currentLocale]; | |
NSArray *countryArray = [NSLocale ISOCountryCodes]; | |
NSMutableDictionary *countriesDictionary = [[NSMutableDictionary alloc] initWithCapacity:countryArray.count]; | |
for (NSString *countryCode in countryArray) { | |
NSString *localizedCountryName = [locale displayNameForKey:NSLocaleCountryCode value:countryCode]; | |
countriesDictionary[countryCode] = localizedCountryName; | |
} | |
NSArray *sortedCodes = [countriesDictionary keysSortedByValueUsingComparator:^(id obj1, id obj2) { | |
NSString *country1 = obj1; | |
NSString *country2 = obj2; | |
return [country1 localizedCompare:country2]; | |
}]; | |
return sortedCodes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment