code: def layer_sort(hmap):
     orderings = {}
     for o in hmap:
        okeys = [get_overlay_spec(o, k, v) for k, v in o.data.items()]
        if len(okeys) == 1 and not okeys[0] in orderings:
           orderings[okeys[0]] = []
        else:
           orderings.update({k: [] if k == v else [v] for k, v in zip(okeys[1:], okeys)})
     return [i for g in sort_topologically(orderings) for i in sorted(g)]