def topological_sort(items,order,key=lambda x:x):
    m,s,l,d = pairs_to_dict(order,key),list(items),[],set()
    while len(s) > 0:
        i = s.pop()
        k = key(i)
        if not k in d:
            if k in m:
                s.append(i)
                s.extend(x for x in m[k] if key(x) != k)
                del m[k]
            else:
                d.add(k)
                l.append(i)
    return list(reversed(l))
items is a list, key maps list elements to hashable keys,
dataset/ETHPy150Open Microsoft/ivy/src/ivy/ivy_utils.py/topological_sort