Sorting in popup menus

Hi, is there an easy way to get popup menu to sort more logically, i.e.

image

This would come as 9, 10, 11, 12…??

thanks

Need to sort using a natural sort algorithm, some pointers to c++ options here.

thx. i was more looking for whether this is easily doable in the popup menu without having to write code to do it…

I’m not aware that PopupMenu does anything other than show the items in the order they were added with addItem.

There is however a natural sort built into JUCE that could be used perhaps? JUCE: StringArray Class Reference

that’s probably it - doh - these are autogenerated popups and it’s probably the source parsing that is doing the sorting - thx for the pointer !

It’s fairly easy to write a function to sort menus:

void sort (PopupMenu& m)
{
    PopupMenu menuCopy (m);

    Array<PopupMenu::Item> items;

    for (PopupMenu::MenuItemIterator i (menuCopy); i.next();)
        items.add (i.getItem());

    struct Sorter
    {
        static int compareElements (const PopupMenu::Item& m1,
                                    const PopupMenu::Item& m2) noexcept
        {
            return m1.text.compareNatural (m2.text);
        }
    };

    Sorter sorter;
    items.sort (sorter);

    m.clear();

    for (const auto& i : items)
        m.addItem (i);
}
1 Like

finally got back onto this - thanks!