Sort a list of formulas by `id`, where `id` represents the accepted
    formula id.

    Parameters
    ----------
    raw_datasets : list of dictionaries
        A list of raw datasets.

    Examples
    --------
    The parameter `raw_datasets` has to be of the format

    >>> rd = [{'is_in_testset': 0,
    ...        'formula_id': 31,
    ...        'handwriting': HandwrittenData(raw_data_id=2953),
    ...        'formula_in_latex': 'A',
    ...        'id': 2953},
    ...       {'is_in_testset': 0,
    ...        'formula_id': 31,
    ...        'handwriting': HandwrittenData(raw_data_id=4037),
    ...        'formula_in_latex': 'A',
    ...        'id': 4037},
    ...       {'is_in_testset': 0,
    ...        'formula_id': 31,
    ...        'handwriting': HandwrittenData(raw_data_id=4056),
    ...        'formula_in_latex': 'A',
    ...        'id': 4056}]
    >>> sort_by_formula_id(rd)

def sort_by_formula_id(raw_datasets):
    """
    Sort a list of formulas by `id`, where `id` represents the accepted
    formula id.

    Parameters
    ----------
    raw_datasets : list of dictionaries
        A list of raw datasets.

    Examples
    --------
    The parameter `raw_datasets` has to be of the format

    >>> rd = [{'is_in_testset': 0,
    ...        'formula_id': 31,
    ...        'handwriting': HandwrittenData(raw_data_id=2953),
    ...        'formula_in_latex': 'A',
    ...        'id': 2953},
    ...       {'is_in_testset': 0,
    ...        'formula_id': 31,
    ...        'handwriting': HandwrittenData(raw_data_id=4037),
    ...        'formula_in_latex': 'A',
    ...        'id': 4037},
    ...       {'is_in_testset': 0,
    ...        'formula_id': 31,
    ...        'handwriting': HandwrittenData(raw_data_id=4056),
    ...        'formula_in_latex': 'A',
    ...        'id': 4056}]
    >>> sort_by_formula_id(rd)
    """
    by_formula_id = defaultdict(list)
    for el in raw_datasets:
        by_formula_id[el['handwriting'].formula_id].append(el['handwriting'])
    return by_formula_id