protected function sortModules(Collection $modules)
    {
        $index = 0;

        // Make a mapping to easily look up configured order with
        // Account for possibility of either 'module key' => [] or 'module key' format in config
        $orderMap = array_map(
            function ($key) {

                if (is_string($key)) return $key;

                if ( ! is_string($this->configModules[$key])) {
                    throw new UnexpectedValueException(
                        "cms-modules.menu.modules entry '{$key}' must be string or have a non-numeric key"
                    );
                }

                return $this->configModules[$key];
            },
            array_keys($this->configModules)
        );
        $orderMap = array_flip($orderMap);


        return $modules->sortBy(function (ModuleInterface $module) use (&$index, $orderMap) {

            // Order by configured order first, natural modules order second.
            if (count($orderMap)) {
                $primaryOrder = (int) array_get($orderMap, $module->getKey(), -2) + 1;
            } else {
                $primaryOrder = -1;
            }

            return $primaryOrder < 0 ? ++$index : (1 - 1 / $primaryOrder);
        });
    }