sortItems

New in version 10.4: sortItems allows sorting of static select items by their values or labels.

sortItems
Type:array
Scope:Display
RenderType:all

The property sortItems allows sorting of static select items by their values or labels.

Built-in orderings are to sort items by their labels or values. It is also possible to define custom sorting via PHP code.

When using grouped select fields with itemGroups, sorting happens on a per-group basis - all items within one group are sorted - as the group ordering is preserved.

Examples

Sort items by a custom method

The following custom method sorts the items by their reversed labels:

class SelectItemSorter
{
    /**
     * Sort items by their reverse titles
     *
     * @param array $items
     */
    public function sortReverseTitles(&$items): void
    {
        @usort(
            $items,
            function ($item1, $item2) {
                return strcasecmp(strrev((string) $item1[0]), strrev((string) $item2[0]));
            }
        );
    }
}