This is a sorting function generator that takes an expression optionally
    prefixed with a "+" (ascending, the default) or "-" (descending) character.

    >>> sorted([{'a': 12}, {'a': 1}, {'a': 4}], sorter("+a"))
    [{'a': 1}, {'a': 4}, {'a': 12}]
    >>> sorted([{'a': 24}, {'a': 16}, {'a': 32}], sorter("-a"))
    [{'a': 32}, {'a': 24}, {'a': 16}]

def sorter(expr):
    """
    This is a sorting function generator that takes an expression optionally
    prefixed with a "+" (ascending, the default) or "-" (descending) character.

    >>> sorted([{'a': 12}, {'a': 1}, {'a': 4}], sorter("+a"))
    [{'a': 1}, {'a': 4}, {'a': 12}]
    >>> sorted([{'a': 24}, {'a': 16}, {'a': 32}], sorter("-a"))
    [{'a': 32}, {'a': 24}, {'a': 16}]
    """
    order = ascending
    if not callable(expr):
        if ',' in expr:
            sorts = map(sorter, expr.split(','))
            return multisorter(*sorts)
        if expr[0] == '-':
            order = descending
            expr = expr[1:]
        elif expr[0] == '+':
            expr = expr[1:]
        expr = expression(expr)
    def _sort(a, b):
        return order(expr(a), expr(b))
    return _sort