def sort_by_list_order(sortlist, reflist, reverse=False, fltr=False,
                       slemap=None):
    def keyfunc(entry):
        if slemap is not None:
            rle = slemap(entry)
        if rle in reflist:
            return reflist.index(rle)
        else:
            return sortlist.index(entry) + len(reflist)
    if fltr:
        if slemap:
            sortlist = filter(lambda x: slemap(x) in reflist, sortlist)
        else:
            sortlist = filter(lambda x: x in reflist, sortlist)
    return sorted(sortlist, key=keyfunc, reverse=reverse)