from typing import OrderedDict


def _to_ordered_dict(d):
    """
    Recursively converts a dict to OrderedDict. 
    This is needed to preserve `(key, value)` ordering
    with iterating through `dict`'s between Python 2 and Python 3.

    Parameters
    ----------
    d : dict
        Dictionary to order.

    Returns
    -------
    OrderedDict
        Recursively order representation of the input dictionary.
    """
    d_ordered = OrderedDict()
    for key, value in sorted(d.items()):
        if isinstance(value, dict):
            d_ordered[key] = _to_ordered_dict(value)
        elif isinstance(value, list) and (all(isinstance(item, dict) for item in value)):
            list_of_ordered_dicts = []
            for item in value:
                list_of_ordered_dicts.append(_to_ordered_dict(item))
            d_ordered[key] = list_of_ordered_dicts
        else:
            d_ordered[key] = value
    return d_ordered