Skip to content

Instantly share code, notes, and snippets.

@ishaq
Last active August 29, 2015 14:01
Show Gist options
  • Save ishaq/45ed5e0201be380b359c to your computer and use it in GitHub Desktop.
Save ishaq/45ed5e0201be380b359c to your computer and use it in GitHub Desktop.
Get a list of all country codes sorted by their localised names
+ (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