VansFannel
VansFannel

Reputation: 45961

Sort [NSDictionary allKeys] alphabetically

I'm developing an iOS 5.0+ app with latest SDK.

I want to show a UIPickerView with a list of countries. These countries will be sorted alphabetically. And when the user select a country, I have to stored its ISO Code.

This is the code I'm using now to get countries' names localized:

+ (NSArray*)countriesNames
{
    NSLocale *locale = [NSLocale currentLocale];
    NSArray *countryArray = [NSLocale ISOCountryCodes];

    NSMutableArray *sortedCountryArray = [[NSMutableArray alloc] init];

    for (NSString *countryCode in countryArray)
    {
        NSString* displayNameString = [locale displayNameForKey:NSLocaleCountryCode
                                                          value:countryCode];
        [sortedCountryArray addObject:displayNameString];
    }
    [sortedCountryArray sortUsingSelector:@selector(localizedCompare:)];

    return sortedCountryArray;
}

But, I need to use something, for example, a NSDictionary; that let me know get ISO Code and localized name.

I have tried to use a NSDictionary instead of a NSArray:

+ (NSDictionary*)countriesNames
{
    NSLocale *locale = [NSLocale currentLocale];
    NSArray *countryArray = [NSLocale ISOCountryCodes];

    NSMutableDictionary* sortedCountryDic = [[NSMutableDictionary alloc] init];

    for (NSString *countryCode in countryArray)
    {
        NSString* displayNameString = [locale displayNameForKey:NSLocaleCountryCode
                                                          value:countryCode];
        [sortedCountryDic setObject:countryCode forKey:displayNameString];

    }

    [[sortedCountryDic allKeys] sortUsingSelector:@selector(localizedCompare:)];

    return sortedCountryDic;
}

But I get a compile time exception: No visible @interface for 'NSArray' declares the selector 'sortUsingSelector:'.

Here:

[[sortedCountryDic allKeys] sortUsingSelector:@selector(localizedCompare:)];

How can I sort allKeys?
Is there any way to get ISOCode using its displayName?

Upvotes: 1

Views: 1829

Answers (1)

lanbo
lanbo

Reputation: 1678

sortUsingSelector: is a method of NSMutableArray not a method of NSArray you can use sortedArrayUsingSelector to sort the array, and you will get a new array with sorted contents

- (NSArray *)sortedArrayUsingSelector:(SEL)comparator


+ (NSArray*)countriesNames
{
    NSLocale *locale = [NSLocale currentLocale];
    NSArray *countryArray = [NSLocale ISOCountryCodes];

    NSMutableDictionary* sortedCountryDic = [[NSMutableDictionary alloc] init];

    for (NSString *countryCode in countryArray)
    {
        NSString* displayNameString = [locale displayNameForKey:NSLocaleCountryCode
                                                          value:countryCode];
        [sortedCountryDic setObject:countryCode forKey:displayNameString];

    }

    return [[sortedCountryDic allKeys] sortedArrayUsingSelector:@selector(localizedCompare:)];    
}

Upvotes: 2

Related Questions