def to_sort_key(table, key):
    if isinstance(key, DeferredSortKey):
        key = key.resolve(table)
    if isinstance(key, ir.SortExpr):
        return key
    if isinstance(key, (tuple, list)):
        (key, sort_order) = key
    else:
        sort_order = True
    if (not isinstance(key, ir.Expr)):
        key = table._ensure_expr(key)
        if isinstance(key, (ir.SortExpr, DeferredSortKey)):
            return to_sort_key(table, key)
    if isinstance(sort_order, str):
        if (sort_order.lower() in ('desc', 'descending')):
            sort_order = False
        elif (not isinstance(sort_order, bool)):
            sort_order = bool(sort_order)
    return SortKey(key, ascending=sort_order).to_expr()
def to_sort_key(table, key):
    if isinstance(key, DeferredSortKey):
        key = key.resolve(table)
    if isinstance(key, ir.SortExpr):
        return key
    if isinstance(key, (tuple, list)):
        (key, sort_order) = key
    else:
        sort_order = True
    if (not isinstance(key, ir.Expr)):
        key = table._ensure_expr(key)
        if isinstance(key, (ir.SortExpr, DeferredSortKey)):
            return to_sort_key(table, key)
    if isinstance(sort_order, str):
        if (sort_order.lower() in ('desc', 'descending')):
            sort_order = False
        elif (not isinstance(sort_order, bool)):
            sort_order = bool(sort_order)
    return SortKey(key, ascending=sort_order).to_expr()