1

I have a list of country names in different languages that I am attempting to sort by their country name. Currently, the sort is based on the index value.

Here is my truncated list of country names:

ADDRESS_COUNTRY_STYLE_TYPES = {}

for language_code in LANGUAGES.iterkeys():
    ADDRESS_COUNTRY_STYLE_TYPES[language_code] = OrderedDict()
    if 'af' in LANGUAGES.iterkeys(): 
        ADDRESS_COUNTRY_STYLE_TYPES['af'][0] = " Kies 'n land of gebied" # Select a country or territory
        ADDRESS_COUNTRY_STYLE_TYPES['af'][1] = "Afganistan" #Afghanistan
        ADDRESS_COUNTRY_STYLE_TYPES['af'][2] = "Åland" #Aland
        ADDRESS_COUNTRY_STYLE_TYPES['af'][3] = "Albanië" #Albania
        ....
        ADDRESS_COUNTRY_STYLE_TYPES['af'][14] = "Australië" #Australia
        ADDRESS_COUNTRY_STYLE_TYPES['af'][15] = "Oostenryk" #Austria
        ADDRESS_COUNTRY_STYLE_TYPES['af'][16] = "Aserbeidjan" #Azerbaijan
        ADDRESS_COUNTRY_STYLE_TYPES['af'][17] = "Bahamas" #Bahamas
        ADDRESS_COUNTRY_STYLE_TYPES['af'][18] = "Bahrein" #Bahrain
        ADDRESS_COUNTRY_STYLE_TYPES['af'][19] = "Bangladesj" #Bangladesh
        ADDRESS_COUNTRY_STYLE_TYPES['af'][20] = "Barbados" #Barbados
        ADDRESS_COUNTRY_STYLE_TYPES['af'][21] = "Wit-Rusland" #Belarus
        ADDRESS_COUNTRY_STYLE_TYPES['af'][22] = "België" #Belgium
        ....

Here is my code I have in my views.py file that calls the country names:

def get_address_country_style_types(available_languages, with_country_style_zero=True):
    address_country_style_types = {}
    preview_labels = {}
    for code, name in available_languages:
        address_country_style_types[code] = ADDRESS_COUNTRY_STYLE_TYPES[code].copy()
        if not with_country_style_zero:
            address_country_style_types[code].pop(0)
        preview_labels[code] = ADDRESS_DETAILS_LIVE_PREVIEW_LABELS[code]
        # in case preview labels are not defined for the language code
        # fall back to 'en', which should always be there
        if len(preview_labels[code]) == 0:
            preview_labels[code] = ADDRESS_DETAILS_LIVE_PREVIEW_LABELS['en']

    address_country_style_types = sorted(address_country_style_types, key=lambda x:x[1])

    return address_country_style_types, preview_labels

The above code only returns the index number in the html drop down list. The issue is with the following line of code (or more to the point my lack of knowledge of how to get it working):

address_country_style_types = sorted(address_country_style_types, key=lambda x:x[1])

How do I return the sorted country list ? Am I using lambda in the correct way here? Should I be using lambda here?

I have been working on this over several days, my coding skills are not very strong, and I have read many related posts to no avail, so any help is appreciated.

I have read this blog about sorting a list of multilingual country names that appear in a form HTML select drop down list - which is essentially what I am attempting to do.

EDIT

Commenting out the line of code below in the code above does return a list of country names, but the country names are sorted by the index value not the country name.

address_country_style_types = sorted(address_country_style_types, key=lambda x:x[1])
user1261774
  • 3,525
  • 10
  • 55
  • 103

1 Answers1

2

I have failed to sort the multi-language country names programatically.

Instead, I copied the list into excel and hit the sort button (based on the translated country name - the index value stays uniform), then copied the data back to the file. Works as expected - just a lot of work.

I hope that this helps someone.

user1261774
  • 3,525
  • 10
  • 55
  • 103
  • If anyone is seeking a js/jq way to alphabetically sort the contenst of a select list, try here: http://stackoverflow.com/questions/12073270/sorting-options-elements-alphabetically-using-jquery – user1261774 Oct 07 '14 at 21:58