def sortedDictLists(d, byvalue=True, onlykeys=None, reverse=False):
    """Return (key list, value list) pair from a dictionary,
    sorted by value (default) or key.
    Adapted from an original function by Duncan Booth.
    """
    if onlykeys is None:
        onlykeys = list(d.keys())
    if byvalue:
        i = [(val, key) for (key, val) in d.items() if key in onlykeys]
        i.sort()
        if reverse:
            i.reverse()
        rvals = [val for (val, key) in i]
        rkeys = [key for (val, key) in i]
    else:
        # by key
        i = [(key, val) for (key, val) in d.items() if key in onlykeys]
        i.sort()
        if reverse:
            i.reverse()
        rvals = [val for (key, val) in i]
        rkeys = [key for (key, val) in i]
    return (rkeys, rvals)