def format_dictionary(adict, sep=', ', sort_first=True):
    """\
    Formats a dictionary into a string of key=value pairs.

    :param adict: dictionary to format
    :param sep: separator. Default ','
    :param sort_first: whether to sort the key-value pairs first before formatting
    :return: a string with the formatted dictionary

    """
    preprocess = sorted if sort_first else lambda x: x
    return sep.join('{}={}'.format(k, v) for k, v in preprocess(iter(adict.items())))