[lang-ref] ( example_insert_into_sorted_array ) ( python )

def test_example_insert_into_sorted_array():
    # bisect.insort
    # Want to add, but not want to sort every time.
    import bisect

    rank = [
        ('Team-A', 974),
        ('Team-B', 962),
        ('Team-C', 985),
    ]
    key_sort_by_score_desc = lambda x: -x[1]
    rank_sorted = sorted(rank, key=key_sort_by_score_desc)

    assert [x[0] for x in rank_sorted] == ['Team-C', 'Team-A', 'Team-B']

    bisect.insort(rank_sorted, ('Team-D', 980), key=key_sort_by_score_desc)

    assert [x[0] for x in rank_sorted] == ['Team-C', 'Team-D', 'Team-A', 'Team-B']