Skip to content

[C++][Python] list_sort - sorting individual ListScalars #48060

Not planned

Description

@MarcoGorelli

Describe the enhancement requested

A list_sort function to sort the elements with each row in a ListArray

Component(s)

Python

Activity

  1. AlenkaF commented on Nov 5, 2025

    @AlenkaF
    Member

    Rows in a ListArray are ListScalar class which do not have an option to be sorted AFAIK.
    But they can be constructed, zero-copy, as arrays which can be sorted. Maybe this kind of sort would only need some extra logic?

    I think it would be a good feature.

    @rok what do you think?

  2. changed the title [-]`list_sort`[/-] [+][C++][Python]`list_sort` - sorting individual ListScalars[/+] on Nov 5, 2025
  3. raisadz commented on Nov 6, 2025

    @raisadz

    Hello, would you be also willing to consider adding aggregate functions to the list methods: list_min, list_max, list_mean, list_median, and list_sum? Thanks

  4. MarcoGorelli commented on Nov 6, 2025

    @MarcoGorelli
    ContributorAuthor

    thanks @AlenkaF !

    Rows in a ListArray are ListScalar class which do not have an option to be sorted AFAIK.

    Just to clarify the request - I'm hoping for an efficient function like

    import pyarrow.compute as pc
    pc.list_sort(my_list_array)

    which does the whole thing in C++, rather than extracting each row as a scalar and sorting each one individually myself (especially from Python!)

  5. AlenkaF commented on Nov 6, 2025

    @AlenkaF
    Member

    does the whole thing in C++, rather than extracting each row as a scalar and sorting each one individually myself (especially from Python!)

    Of course! This clarification was needed though, thanks =)
    I was thinking we might not need a separate kernel in C++ but do it efficiently with existing ones. Not sure though!

    would you be also willing to consider adding aggregate functions to the list methods: list_min, list_max, list_mean, list_median, and list_sum?

    I would personally support additions of new kernels but I have to point out this is an effort that would need developer help from the community (meaning, contributions are most welcome! :) )

  6. AlenkaF commented on Nov 7, 2025

    @AlenkaF
    Member

    I was thinking we might not need a separate kernel in C++ but do it efficiently with existing ones. Not sure though!

    After talking to Rok and looking at the docs I understand better that sorting values "per row" in a list array would need a new kernel due to having all values of the list stored in one buffer. The kernel would need to take offsets into account and sort values in separate intervals.

  7. rok commented on Nov 7, 2025

    @rok
    Member

    I'm slightly surprised we don't have this yet, not even as an aggregation. It would be nice to add it as a C++ kernel indeed.

    @raisadz for the min/max/mean/median/sum, you could use aggregation kernels, though I'm sure this can be done more efficiently working directly on the ListArray:

    import pyarrow as pa
    import pyarrow.compute as pc
    arr = pa.array([[1, 2, None, 0], [7, 7, None, 0], [1, 2, 3]])
    offsets = pc.list_parent_indices(arr)
    table = pa.Table.from_arrays([arr.values, offsets], names=["values", "offsets"])
    table.group_by(["offsets"]).aggregate([("values", "min")])
    
    pyarrow.Table
    offsets: int64
    values_min: int64
    ----
    offsets: [[0,1,2]]
    values_min: [[0,0,1]]
  8. changed the title [-][C++][Python]`list_sort` - sorting individual ListScalars[/-] [+][C++][Python] `list_sort` - sorting individual ListScalars[/+] on Nov 9, 2025
  9. raisadz commented on Nov 10, 2025

    @raisadz

    I'm slightly surprised we don't have this yet, not even as an aggregation. It would be nice to add it as a C++ kernel indeed.

    @raisadz for the min/max/mean/median/sum, you could use aggregation kernels, though I'm sure this can be done more efficiently working directly on the ListArray:

    import pyarrow as pa
    import pyarrow.compute as pc
    arr = pa.array([[1, 2, None, 0], [7, 7, None, 0], [1, 2, 3]])
    offsets = pc.list_parent_indices(arr)
    table = pa.Table.from_arrays([arr.values, offsets], names=["values", "offsets"])
    table.group_by(["offsets"]).aggregate([("values", "min")])

    pyarrow.Table
    offsets: int64
    values_min: int64

    offsets: [[0,1,2]]
    values_min: [[0,0,1]]

    Thanks a lot for pointing me to the aggregation kernels @rok, it seems to work well for my task!

  10. rok commented on Nov 10, 2025

    @rok
    Member

    Great to hear @raisadz ! Do keep in mind aggregate kernels here might be less optimal than what's possible to do operating directly on a ListArray and it's perhaps still worth investigating c++ kernels for your usecase.

  11. github-actions commented on Nov 11, 2026

    @github-actions

    This issue has been marked as stale because it has had no activity in the past 365 days. Please remove the stale label or comment below, or this issue will be closed in 14 days. If this improvement is still desired but has no current owner, please add the 'Status: needs champion' label.

  12. added
    Status: stale-warning
    on Nov 11, 2026
  13. rok commented on Dec 2, 2026

    @rok
    Member

    @MarcoGorelli is this something that would be useful?

  14. MarcoGorelli commented on Dec 4, 2026

    @MarcoGorelli
    ContributorAuthor

    if it would be more efficient than the workaround suggested in #48060 (comment), then yes, definitely

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: PythonStatus: stale-warningType: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions