Skip to content

daft.functions.list_sort#

list_sort #

list_sort(list_expr: Expression, desc: bool | Expression | None = None, nulls_first: bool | Expression | None = None) -> Expression

Sorts the inner lists of a list column.

Parameters:

Name Type Description Default
list_expr List Expression

expression to sort

required
desc bool | Expression | None

(bool | Boolean Expression) Whether to sort in descending order. Defaults to false. Pass in a boolean column to control for each row.

None
nulls_first bool | Expression | None

(bool | Boolean Expression) Whether to put nulls first. Defaults to false (nulls last). Pass in a boolean column to control for each row.

None

Returns:

Name Type Description
Expression List Expression

an expression with the sorted lists

Examples:

1
2
3
4
>>> import daft
>>> from daft.functions import list_sort
>>> df = daft.from_pydict({"a": [[1, 3], [4, 2], [6, 7, 1]]})
>>> df.select(list_sort(df["a"])).show()
╭─────────────╮
│ a           │
│ ---         │
│ List[Int64] │
╞═════════════╡
│ [1, 3]      │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ [2, 4]      │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ [1, 6, 7]   │
╰─────────────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/list.py
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
def list_sort(
    list_expr: Expression, desc: bool | Expression | None = None, nulls_first: bool | Expression | None = None
) -> Expression:
    """Sorts the inner lists of a list column.

    Args:
        list_expr (List Expression): expression to sort
        desc: (bool | Boolean Expression) Whether to sort in descending order. Defaults to false. Pass in a boolean column to control for each row.
        nulls_first: (bool | Boolean Expression) Whether to put nulls first. Defaults to false (nulls last). Pass in a boolean column to control for each row.

    Returns:
        Expression (List Expression): an expression with the sorted lists

    Examples:
        >>> import daft
        >>> from daft.functions import list_sort
        >>> df = daft.from_pydict({"a": [[1, 3], [4, 2], [6, 7, 1]]})
        >>> df.select(list_sort(df["a"])).show()
        ╭─────────────╮
        │ a           │
        │ ---         │
        │ List[Int64] │
        ╞═════════════╡
        │ [1, 3]      │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ [2, 4]      │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ [1, 6, 7]   │
        ╰─────────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

    """
    return Expression._call_builtin_scalar_fn("list_sort", list_expr, desc=desc, nulls_first=nulls_first)