def gsort(g, names):
    if len(names) < 2:
        return names

    empty = set()
    nset = set(names)
    final = list(names)
    ups = {}

    for name in names:
        downs = [v for u,v in nx.dfs_edges(g, name) if v in nset]
        for d in downs:
            if d not in ups.get(name, empty):  # handle cycles
                ups.setdefault(d, set()).add(name)

    i = 0
    while True:
        tmp = final[:i]
        tset = set(tmp)
        unames = [f for f in final[i+1:] if f in ups.get(final[i],empty) and
                     f not in tset]
        if unames:
            tmp.extend(unames)
            tmp.extend([f for f in final[i:] if f not in unames])
        else:
            tmp.extend(final[i:])
            i += 1

        if i == len(names):
            break
        final = tmp

    return final
Testing the GET repositories/?name= API
dataset/ETHPy150Open OpenMDAO/OpenMDAO-Framework/openmdao.main/src/openmdao/main/depgraph.py/gsort--crossover with--dataset/ETHPy150Open reviewboard/reviewboard/reviewboard/webapi/tests/test_repository.py/ResourceListTests.test_get_repositories_with_name