Sorts the vector.
    If the field parameter is provided then the sort
    operators on a vector of structs where the sort key
    is the field of the struct.

    Args:
      expr (WeldObject)
      field (Int)

def sort(expr, field = None, keytype=None, ascending=True):
    """
    Sorts the vector.
    If the field parameter is provided then the sort
    operators on a vector of structs where the sort key
    is the field of the struct.

    Args:
      expr (WeldObject)
      field (Int)
    """
    weld_obj = WeldObject(encoder_, decoder_)

    expr_var = weld_obj.update(expr)
    if isinstance(expr, WeldObject):
        expr_var = expr.obj_id
        weld_obj.dependencies[expr_var] = expr

    if field is not None:
        key_str = "x.$%s" % field
    else:
        key_str = "x"

    if not ascending:
        # The type is not necessarily f64.
        key_str = key_str + "* %s(-1)" % keytype

    weld_template = """
    sort(%(expr)s, |x| %(key)s)
    """
    weld_obj.weld_code = weld_template % {"expr":expr_var, "key":key_str}
    return weld_obj