# Buggy code
def handle_plugins_loaded(
    startup=False, initialize_implementations=True, force_reload=None
):
    if not startup:
        return

    sorted_disabled_from_overlays = sorted(
        [(key, value[0], value[1]) for key, value in disabled_from_overlays.items()],
        key=lambda x: (x[2] is None, x[2], x[0]),
    )

    disabled_list = pm.plugin_disabled_list
    already_processed = []
    for name, addons, _ in sorted_disabled_from_overlays:
        if not name in disabled_list and not name.endswith("disabled"):
            for addon in addons:
                if addon in disabled_list:
                    continue

                if addon in already_processed:
                    logger.info(
                        "Plugin {} wants to disable plugin {}, but that was already processed".format(
                            name, addon
                        )
                    )

                if not addon in already_processed and not addon in disabled_list:
                    disabled_list.append(addon)
                    logger.info(
                        "Disabling plugin {} as defined by plugin {}".format(
                            addon, name
                        )
                    )
            already_processed.append(name)


# Traceback (TypeError)
2019-10-08 14:14:51,399 - octoprint.server.preemptive_cache - INFO - Preemptively caching / (ui _default) for {'base_url': 'http://192.168.0.113:5000/', 'path': '/', 'query_string': 'l10n=en'}
[2019-10-08 14:14:51,556] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "c:\octoprint\venv\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "c:\octoprint\venv\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "c:\octoprint\venv\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "c:\octoprint\venv\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "c:\octoprint\venv\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "c:\octoprint\venv\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "c:\octoprint\venv\lib\site-packages\octoprint\server\views.py", line 181, in index
fetch_template_data(refresh=force_refresh)
File "c:\octoprint\venv\lib\site-packages\octoprint\server\views.py", line 755, in fetch_template_data
sorted_missing = sorted(missing_in_order, key=key_func)
TypeError: '<' not supported between instances of 'str' and 'NoneType'
[2019-10-08 14:14:51,743] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "c:\octoprint\venv\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "c:\octoprint\venv\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "c:\octoprint\venv\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "c:\octoprint\venv\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "c:\octoprint\venv\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "c:\octoprint\venv\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "c:\octoprint\venv\lib\site-packages\octoprint\server\views.py", line 181, in index
fetch_template_data(refresh=force_refresh)
File "c:\octoprint\venv\lib\site-packages\octoprint\server\views.py", line 755, in fetch_template_data
sorted_missing = sorted(missing_in_order, key=key_func)
TypeError: '<' not supported between instances of 'str' and 'NoneType'
[2019-10-08 14:14:51,884] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "c:\octoprint\venv\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "c:\octoprint\venv\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "c:\octoprint\venv\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "c:\octoprint\venv\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "c:\octoprint\venv\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "c:\octoprint\venv\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "c:\octoprint\venv\lib\site-packages\octoprint\server\views.py", line 181, in index
fetch_template_data(refresh=force_refresh)
File "c:\octoprint\venv\lib\site-packages\octoprint\server\views.py", line 755, in fetch_template_data
sorted_missing = sorted(missing_in_order, key=key_func)
TypeError: '<' not supported between instances of 'str' and 'NoneType'

# Fixed code
def handle_plugins_loaded(
    startup=False, initialize_implementations=True, force_reload=None
):
    if not startup:
        return

    from octoprint.util import sv

    sorted_disabled_from_overlays = sorted(
        [(key, value[0], value[1]) for key, value in disabled_from_overlays.items()],
        key=lambda x: (x[2] is None, sv(x[2]), sv(x[0])),
    )

    disabled_list = pm.plugin_disabled_list
    already_processed = []
    for name, addons, _ in sorted_disabled_from_overlays:
        if not name in disabled_list and not name.endswith("disabled"):
            for addon in addons:
                if addon in disabled_list:
                    continue

                if addon in already_processed:
                    logger.info(
                        "Plugin {} wants to disable plugin {}, but that was already processed".format(
                            name, addon
                        )
                    )

                if not addon in already_processed and not addon in disabled_list:
                    disabled_list.append(addon)
                    logger.info(
                        "Disabling plugin {} as defined by plugin {}".format(
                            addon, name
                        )
                    )
            already_processed.append(name)