import six
from typing import OrderedDict


def ordered_recurse(value):
    """Recursively order nested dicts and lists at all levels."""
    if isinstance(value, list):
        return [ordered_recurse(v) for v in value]
    if isinstance(value, dict):
        items = sorted(
            ((key, ordered_recurse(value))
             for key, value in six.iteritems(value)),
            key=lambda i: i[0],
        )
        return OrderedDict(items)

    return value