def match_sort_key(match):
    """Function used to sort matches in chronological order, first by level, then by match set.
        Returns a sorting key value for the provided match model.
    """
    levels = {'qm': 0, 'ef': 1000, 'qf': 2000, 'sf': 3000, 'f': 4000}
    key = levels[match['comp_level']]
    key += 100 * match['set_number'] if match['comp_level'] != 'qm' else 0
    key += match['match_number']
    return key
