How to sort items of OptionButton alphabetically?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MaaaxiKing

I have an OptionButton with Items “c”, “d”, “b”, “t”, “a” in this order. How to sort them (all items) alphabetically in code?

Or is it a bug that $OptionButton.items.sort() doesn’t work?

Have you tried assigning it to a temporary array, sorting that array, and assigning it back to item?

Ertain | 2020-06-06 18:48

The problem is there is no get_items() function or items.text.

MaaaxiKing | 2020-06-06 18:59

OptionButton — Godot Engine (stable) documentation in English

int 	 	get_item_count ( ) const
Texture 	get_item_icon ( int idx ) const
int 	 	get_item_id ( int idx ) const
int 	 	get_item_index ( int id ) const
Variant 	get_item_metadata ( int idx ) const
String 	 	get_item_text ( int idx ) const

Becbunzen | 2020-06-06 20:53

:bust_in_silhouette: Reply From: Ertain

Thanks to Becbunzen for pointing out where the methods are in the documentation. To sort them alphabetically, you may have to write it like this:

# Somewhere at the top of the script.
onready var options_button: OptionsButton = $/path/to/OptionsButton

# This function will work if the list is made of *only* String items. It can't manage separators.
func alphabetize():
    # Have a dictionary hold all of the necessary information. Assign the text strings as keys. Note that the text strings *must* be unique from the other items.
    var unorganized_items: Dictionary = {}
    
    for item_number in options_button.get_item_count():
        # Get all of the information for each item.
        var text: String = options_button.get_item_text( item_number )
        var icon: Texture = options_button.get_item_icon( item_number )
        # Put them into our dictionary.
        unorganized_items[text] = icon
    # Clear out the options button so that we can put in our sorted list.
    options_button.clear()
    # Populate our options button with the newly alphabetized list.
    var sorted_keys: Array = unorganized_items.keys()
    # For some odd reason, this method has to be done separately.
    sorted_keys.sort()
    # Go through the sorted keys and add the new items to our options button.
    for text in sorted_keys:
        options_button.add_icon_item( unorganized_items[text], text )

As I have noted in the function, the code can only take a list of String items; no separators.

:bust_in_silhouette: Reply From: Chevon

Sometimes the more complex problems have the simplest solutions.
You are the programmer you know all the possible items the user can have.

Just hard code an array with all the possible items as strings in alphabetical order
Then just refer to it with a double for for loop.

First loop checking all the possible items, second loop searching the players items. In this case the button name
If theres a match append a temperary array/ dictionary depending on use case
Then assign the temporary variable as the new list after its complete.

Better yet just use a single loop over the array of all the possible items from the alphabetical array then use the has() function on your player items dictionary then do it that way