def sort_by_value(histogram, topK=None, filterFn=None):
    """ Return a sorted list of (value, tag) pairs from a given histogram.

        If filter is specified the results are filtered using that function
        If topK is specified, only return topK results
    """
    res = reversed(sorted(
        [(val, tag) for tag, val in histogram.items()],
        key=lambda pair: pair[0]
    ))

    if filter is not None:
        res = filter(filterFn, res)

    if topK is not None:
        return list(res)[:topK]
    else:
        return list(res)