Skip to content

Instantly share code, notes, and snippets.

@stuffmc
Created April 20, 2010 12:04
Show Gist options
  • Save stuffmc/372349 to your computer and use it in GitHub Desktop.
Save stuffmc/372349 to your computer and use it in GitHub Desktop.
// First get the list of ISO Codes
NSArray *countries = [NSLocale ISOCountryCodes];
// Then create an empty array for the list of country with both code and name
NSMutableArray *namedCountries = [NSMutableArray arrayWithCapacity:[countries count]];
NSUInteger countryIndex = 0;
// Loop over the codes and have their localized names
for (NSString *code in countries) {
NSString *name = [[NSLocale currentLocale] displayNameForKey:NSLocaleCountryCode value:code];
NSDictionary *countryDictionary = [NSDictionary dictionaryWithObjectsAndKeys:code, @"code", name, @"name", nil];
[namedCountries insertObject:countryDictionary atIndex:countryIndex++];
}
// Now sort on the names
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedCountries = [namedCountries sortedArrayUsingDescriptors:sortDescriptors];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment