Sort dropdown items

I enter a set if alphabetically ordered strings into a dropdown. However when I open my dash app the dropdown items are no longer in alphabetical order… they seem to be random

Dropdown options takes a list of {‘label’: string|number, ‘value’:string|number}, and it will respect the order of the given list.
If you are using set to order a list of strings alphabetically, using list on this set will reverse it.

Make sure that your options are in the correct format,
Check the order of the options as they come in, (print your options or smthg),
Good luck!

Just to expand on what @Ncohen said, you can use Python’s in-built sorted function to sort your options by label rather than hard code it.

I.e. if you have something like this

options = [
    {"label": "Zappa", "value": 3},
    {"label": "Alpha", "value": 1},
    {"label": "Bravo", "value": 2},

then you can sort like this

sorted_options = sorted(options, key=lambda x: x["label"])

Great this worked thanks! Here is my code for future problems with this:

deal_ids = set(deal_terms['deal_id'])
dcc.Dropdown(id = 'my-dropdown', 
                 options = sorted([{'label': i, 'value': i} for i in deal_ids], key = lambda x: x['label']),
1 Like