import re


def extract_bibtex_entries(master_bib_file, citekeys):
    """

    Extract bibtex entries from master_bib_file that have certain citekeys.
    Return the entries sorted by citekey.

    """
    if len(citekeys) == 0:
        return []
    with open(master_bib_file, "r", encoding="utf-8") as file:
        master_bib = file.read()
    bibtex_entries = []
    for citekey in citekeys:
        match = re.search(
            r"@.*?\{" + citekey + r"[\s\S]+?\n\}\n", master_bib, re.UNICODE
        )
        if match is None:
            print(f"-> Citekey '{citekey}' was not found in {master_bib_file}")
        else:
            bibtex_entries.append((citekey, match.group(0)))
    return [entry[1] for entry in sorted(bibtex_entries)]