from typing import OrderedDict


def custom_sort_exps(exps):
    """
    Sort the experiments according to JIRA LCLSECSD-210
    Active first, OPS next and then descending run period/alphabetical within that run period
    """
    # Python's sort is stable; so we sort multiple times lowest attr first
    exps = OrderedDict(sorted(exps.items(), key=lambda x : x[1]["name"]))
    # print( [ v["name"] for k, v in exps.items() ] )
    exps = OrderedDict(sorted(exps.items(), key=lambda x : x[1]["name"][-2:], reverse=True))
    exps = OrderedDict(sorted(exps.items(), key=lambda x : not x[1].get("instrument", "") == "OPS"))
    exps = OrderedDict(sorted(exps.items(), key=lambda x : not x[1].get("is_active", False)))
    return exps